Least Number of Unique Integers after K Removals - Problem
Given an array of integers arr and an integer k, find the least number of unique integers after removing exactly k elements.
You need to remove exactly k elements from the array, and your goal is to minimize the number of distinct integers that remain.
Strategy: To minimize unique integers, remove elements with the lowest frequencies first, as this eliminates entire unique values faster than removing high-frequency elements.
Input & Output
Example 1 — Basic Case
$
Input:
arr = [4,3,1,1,3], k = 2
›
Output:
2
💡 Note:
Count frequencies: 4 appears 1 time, 3 appears 2 times, 1 appears 2 times. Remove the element with frequency 1 (value 4) using 1 removal. We have 1 more removal left but can't eliminate any complete group. Result: 2 unique values remain (3 and 1).
Example 2 — Complete Elimination
$
Input:
arr = [5,5,4], k = 2
›
Output:
1
💡 Note:
Count frequencies: 5 appears 2 times, 4 appears 1 time. Remove value 4 (1 removal), then remove one instance of 5 (1 more removal). We used 2 removals total. Result: 1 unique value remains (5).
Example 3 — Minimum Input
$
Input:
arr = [1], k = 1
›
Output:
0
💡 Note:
Only one element exists with frequency 1. Remove it completely using 1 removal. Result: 0 unique values remain.
Constraints
- 1 ≤ arr.length ≤ 105
- 1 ≤ arr[i] ≤ 109
- 1 ≤ k ≤ arr.length
Visualization
Tap to expand
Understanding the Visualization
1
Input Analysis
Array [4,3,1,1,3] with k=2 removals needed
2
Frequency Strategy
Count frequencies and remove rarest elements first
3
Optimal Result
Remove value 4 completely → 2 unique values remain
Key Takeaway
🎯 Key Insight: Always remove the rarest elements first - this eliminates entire unique values faster than removing common elements partially
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code