Exercise: Sort by Occurrences

Practice with a real interview exercise.

We'll cover the following

Write an OrderByOccurrences method

For this exercise, let’s stop talking about movies. We’ve covered everything we need to solve this exercise so far in this chapter.

Author’s Note: This is an interview question I got when applying for my last position as a software engineer.

Write an OrderByOccurrences method to sort a collection of numbers by the frequency of occurrences of its elements.

For example, the list [3,2,2,3,3,1] after sorted by occurrences would be [1,2,2,3,3,3] since 1 appears once; 2, twice; and 3, three times. If two numbers have the same frequency, they should be in ascending order.

To complete this exercise, fill in the body of the OrderByOccurrences method.

Get hands-on with 1000+ tech skills courses.