logo CodeStepByStep logo

kMostFrequent

Language/Type: Java arrays traversals
Related Links:

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.

Method: Write a Java method as described, not a complete program or class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.