Least Number of Unique Integers after K Removals - Problem

You're given an array of integers arr and an integer k. Your goal is to minimize the number of unique integers in the array by removing exactly k elements.

Think of it as a strategic elimination game: you have k removal operations, and you want to use them wisely to reduce the variety of numbers as much as possible. The key insight is that removing all occurrences of a number reduces the unique count by 1, while removing some (but not all) occurrences doesn't change the unique count at all.

For example, if you have [4,3,1,1,3,3,2] and can remove k=3 elements, you could remove one occurrence each of 4, 1, and 2, leaving [3,1,3,3] with 2 unique integers. But a better strategy would be to completely eliminate 4 and 2 (2 removals) and one occurrence of 1 (1 removal), leaving [3,1,3,3] with still 2 unique integers, or even better - remove 4, 2, and both 1's to get [3,3,3] with just 1 unique integer.

Input & Output

example_1.py β€” Basic Case
$ Input: arr = [5,5,4], k = 1
β€Ί Output: 1
πŸ’‘ Note: Remove the single occurrence of 4. The remaining array is [5,5] which has 1 unique integer (5).
example_2.py β€” Multiple Strategy
$ Input: arr = [4,3,1,1,3,3,2], k = 3
β€Ί Output: 2
πŸ’‘ Note: Remove 4 (1 removal), remove 2 (1 removal), remove one 1 (1 removal). The remaining array is [3,1,3,3] with 2 unique integers. This is optimal.
example_3.py β€” Remove All Elements
$ Input: arr = [1], k = 1
β€Ί Output: 0
πŸ’‘ Note: Remove the only element. The remaining array is empty, so there are 0 unique integers.

Constraints

  • 1 ≀ arr.length ≀ 105
  • 1 ≀ arr[i] ≀ 109
  • 0 ≀ k ≀ arr.length
  • k is always valid (you can always remove exactly k elements)

Visualization

Tap to expand
🎡 Music Library Cleanup: Remove k=3 songs to minimize artistsCollection: [JazzΓ—1, RockΓ—3, PopΓ—2, ClassicalΓ—1] β†’ Songs: [J, R, P, P, R, R, C]Strategy: Remove complete rare artists rather than partial popular onesπŸ“Š Step 1: Count CollectionJazz1 songClassical1 songPop2 songsRock3 songsFrequency count: {Jazz:1, Classical:1, Pop:2, Rock:3}Sorted frequencies: [1, 1, 2, 3]From artists: [Jazz, Classical, Pop, Rock]🎯 Step 2: Strategic Removal (k=3)βœ“ Remove Jazz (1)k=2 left, 3 artists remainβœ“ Remove Classical (1)k=1 left, 2 artists remainβœ— Can't remove Pop (2)Need k=2, have k=1Remaining: Pop (2 songs) + Rock (3 songs)Final result: 2 unique artistsOptimal strategy: Always eliminate rarest items first!Partial removal doesn't reduce unique countπŸ’‘ Key Insight: Greedy Choice is OptimalRemoving all occurrences of a rare item (frequency f) eliminates 1 unique item with f removals.Partially removing a common item uses removals but doesn't eliminate any unique items.Therefore: Always prioritize complete elimination of rarest items first!πŸ† ComplexityTime: O(n log n) - counting + sortingSpace: O(n) - frequency mapOptimal greedy solution!
Understanding the Visualization
1
Count Your Collection
Count how many songs each artist has (frequency counting)
2
Prioritize by Rarity
Sort artists by number of songs, rarest first
3
Strategic Removal
Remove entire discographies of rare artists until you can't remove a complete one
Key Takeaway
🎯 Key Insight: The greedy approach of eliminating complete rare groups before touching common ones is mathematically optimal - partial removals never reduce the unique count, so we should always prioritize complete eliminations.
Asked in
Google 42 Amazon 38 Meta 31 Apple 25
67.2K Views
High Frequency
~18 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen