sher_Ka_Code

 avatar
bruteCoder
java
2 months ago
640 B
1
Indexable
Never
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;
    }
}
Leave a Comment