Ctrl K

Rare Values First

Easy
You are given an integer array values. Rearrange it so that rarer numbers come first: all numbers that occur once, then those that occur twice, and so on. Copies of the same number stay together. If two different numbers occur the same number of times, the larger one comes first. Return the rearranged array.
Example 1
values=[4, 9, 4, 7, 9, 4] → [7, 9, 9, 4, 4, 4]

7 appears once, 9 twice and 4 three times, so 7 goes first, then both 9s, then the three 4s.

Example 2
values=[-2, 5, 3, 5, -2, 3] → [5, 5, 3, 3, -2, -2]

All three numbers appear twice, so they go in decreasing order: 5, 3, -2.

Constraints
  • 1 ≤ len(values) ≤ 100
  • -100 ≤ values[i] ≤ 100
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsThe Candidate With Most Votes EasyDoes Any Value Repeat EasyThe Lost Ticket EasyValues in Both Lists EasyShared Values With Repeats Easy
loading Python…
Input
values=[4, 9, 4, 7, 9, 4]
Expected
[7, 9, 9, 4, 4, 4]

Run checks the first 3 tests in your browser. Submit checks all 116 tests on the server, hidden ones included.