Need an additional test case {1,1,2,2,3,3}


  • 0
    S

    //My Java code
    public List<Integer> topKFrequent(int[] nums, int k){
    List<Integer>[] bucket = new List[nums.length+1];
    Map<Integer,Integer> frequencyMap = new HashMap<Integer,Integer>();

    for (int n:nums){
    	frequencyMap.put(n, frequencyMap.getOrDefault(n,0)+1);
    
    }
    
    for (int key:frequencyMap.keySet()){
    	int frequenc = frequencyMap.get(key);
    	if (bucket[frequenc] == null) {
    		bucket[frequenc] = new ArrayList<>();
    	}
    	bucket[frequenc].add(key);
    }
    List<Integer> res = new ArrayList<>();
    
    for (int pos = bucket.length-1;pos >=0 && res.size()<k;pos--){
    	if (bucket[pos]!=null){
    		res.addAll(bucket[pos]);
    	}
    }
    return res;
    

    }
    // It passes all the test cases on the website but when it comes to {1,1,2,2,3,3}, it will return{1,2,3} which should be {1,2} or {1,3} or {2,3}.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.