sher_Ka_Code

 avatar
bruteCoder
java
2 years ago
640 B
4
Indexable
class Solution 
{
    //Function to find all elements in array that appear more than n/k times.
    public int countOccurence(int[] arr, int n, int k) 
    {
        // your code here,return the answer
        HashMap<Integer,Integer> map = new HashMap<>();
        int threshold = n/k;
        Set<Integer> set = new HashSet<>();
        int count = 0 ;
        
        for(int el : arr)
        {
            map.put(el,map.getOrDefault(el,0)+1);
            
            if(!set.contains(el) && map.get(el) > threshold) {count+=1;
                set.add(el);
            }
            
        }
        
        return count;
    }
}
Editor is loading...
Leave a Comment