All problems

Popular First

Easy · hash map

You are given a list of integers nums. Rearrange it so that values that appear more often in nums come first.

Return the rearranged list. It has the same length as nums and contains exactly the same values with the same counts.

Examples

Example 1

Input: nums = [4, 1, 4, 2, 1, 4]

Output: [4, 4, 4, 1, 1, 2]

4 appears three times, 1 appears twice and 2 appears once, so they come in that order.

Example 2

Input: nums = [3, 1, 2]

Output: [1, 2, 3]

Every value appears once, so the tie is broken by putting smaller values first.

Example 3

Input: nums = [5, -2, 5, -2, 7]

Output: [-2, -2, 5, 5, 7]

-2 and 5 both appear twice, and -2 is smaller, so it goes first. 7 appears once and comes last.

Constraints