Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find least number of unique integers after K removals using Python
Given an array of integers and a number k, we need to find the minimum number of unique elements remaining after removing exactly k elements. The strategy is to remove elements with the lowest frequency first to minimize unique elements.
For example, if we have nums = [5,4,2,2,4,4,3] and k = 3, we can remove 5 (frequency 1), 3 (frequency 1), and one occurrence of 2 (frequency 2). This leaves us with 2 unique elements: 2 and 4.
Algorithm
The approach involves these steps ?
- Count frequency of each element using a dictionary
- Sort frequencies in ascending order
- Remove elements starting from lowest frequency
- Return remaining unique elements count
Implementation
def find_least_unique_after_removals(nums, k):
# Count frequency of each element
frequency_map = {}
for num in nums:
if num not in frequency_map:
frequency_map[num] = 1
else:
frequency_map[num] += 1
# Initial count of unique elements
unique_count = len(frequency_map)
# Sort frequencies in ascending order
for frequency in sorted(frequency_map.values()):
k -= frequency
if k < 0:
# Cannot remove all occurrences of this element
return unique_count
else:
# Can remove all occurrences of this element
unique_count -= 1
return unique_count
# Test the function
nums = [5, 4, 2, 2, 4, 4, 3]
k = 3
result = find_least_unique_after_removals(nums, k)
print(f"Minimum unique elements after {k} removals: {result}")
Minimum unique elements after 3 removals: 2
How It Works
Let's trace through the example step by step ?
nums = [5, 4, 2, 2, 4, 4, 3]
k = 3
# Step 1: Count frequencies
frequency_map = {5: 1, 4: 3, 2: 2, 3: 1}
print("Frequencies:", frequency_map)
# Step 2: Sort frequencies
sorted_frequencies = sorted(frequency_map.values())
print("Sorted frequencies:", sorted_frequencies)
# Step 3: Remove elements with lowest frequency first
unique_count = 4 # Initial unique elements
remaining_k = k
for freq in sorted_frequencies:
print(f"Can remove {freq} elements, k remaining: {remaining_k}")
if remaining_k >= freq:
remaining_k -= freq
unique_count -= 1
print(f"Removed all occurrences, unique count: {unique_count}")
else:
print(f"Cannot remove all occurrences, final count: {unique_count}")
break
print(f"Final result: {unique_count}")
Frequencies: {5: 1, 4: 3, 2: 2, 3: 1}
Sorted frequencies: [1, 1, 2, 3]
Can remove 1 elements, k remaining: 3
Removed all occurrences, unique count: 3
Can remove 1 elements, k remaining: 2
Removed all occurrences, unique count: 2
Can remove 2 elements, k remaining: 1
Cannot remove all occurrences, final count: 2
Final result: 2
Alternative Implementation Using Counter
We can simplify the frequency counting using Python's Counter ?
from collections import Counter
def find_least_unique_optimized(nums, k):
# Count frequencies using Counter
freq_counter = Counter(nums)
unique_count = len(freq_counter)
# Sort frequencies and remove from lowest
for frequency in sorted(freq_counter.values()):
if k >= frequency:
k -= frequency
unique_count -= 1
else:
break
return unique_count
# Test with different examples
test_cases = [
([5, 4, 2, 2, 4, 4, 3], 3),
([1, 1, 1, 2, 2, 3], 4),
([1, 2, 3, 4, 5], 2)
]
for nums, k in test_cases:
result = find_least_unique_optimized(nums, k)
print(f"nums: {nums}, k: {k} ? result: {result}")
nums: [5, 4, 2, 2, 4, 4, 3], k: 3 ? result: 2 nums: [1, 1, 1, 2, 2, 3], k: 4 ? result: 1 nums: [1, 2, 3, 4, 5], k: 2 ? result: 3
Time and Space Complexity
| Aspect | Complexity | Explanation |
|---|---|---|
| Time | O(n log n) | Counting frequencies O(n) + sorting O(n log n) |
| Space | O(n) | Dictionary to store frequency mapping |
Conclusion
To minimize unique elements after k removals, always remove elements with the lowest frequency first. Use a frequency map and sort frequencies to implement this greedy strategy efficiently.
