id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

kMostFrequent


Write a method named kMostFrequent that accepts an array of integers and an integer k and returns an array of the k unique values in the array that occur most frequently. For example, if the array a stores {7, 4, 5, 1, 4, 4, 5, 6} and k is 2, the call of kMostFrequent(a, k) should return {4, 5} because there are three 4s and two 5s and those are the most frequently occurring elements. You should return the values sorted in decreasing order by number of occurrences. You may assume that k is a positive integer, that there are at least k unique values in the array, and that there will not be any ties for most frequent.


kMostFrequent([7, 4, 5, 1, 4, 4, 5, 6], 2) → [4, 5]
kMostFrequent([0, 8, 3, 8, 3, 8, 3, 3], 3) → [3, 8, 0]
kMostFrequent([1, 2, 3, 1, 1, 2, 4, 3, 1, 2, 2, 1], 2) → [1, 2]

...Save, Compile, Run (ctrl-enter)

public int[] kMostFrequent(int[] arr, int k) { }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Difficulty: 200 Post-solution available

Copyright Nick Parlante 2017 - privacy