Find Unique Element Geeks For Geeks
Problem Link:
https://practice.geeksforgeeks.org/problems/find-unique-element2632/1
Solution :
HashMap<Integer,Integer>map=new HashMap<Integer,Integer>();
for(int i=0;i<n;i++){
if(map.containsKey(a[i])){
map.put(a[i],map.get(a[i])+1);
}
else{
map.put(a[i],1);
}
}
int c=0;
for(int a1:map.keySet()){
int b=map.get(a1);
if(b%k!=0){
c=a1;
break;
}
}
return c;
Time:O(N)[Traversal of given array][GFG Time:1/10.3]
Space:O(N)[Storing all entries in hashmap but the size of the map will be less than n.]
Approach:
1. Traverse the given array and store the elements in the map with their frequency.
2. Apply for each loop on the key set of map and see whether the frequency of the element is divisible by k or not. If not then break the loop and print the key ie element.
3. Return element.
"Thanks For Reading.😀"
"Share Further To Increase Knowledge Treasure.😊"
Comments
Post a Comment