#C1282. Unique Array Reduction
Unique Array Reduction
Unique Array Reduction
You are given an array of integers. Your task is to remove a subset of elements (not necessarily contiguous) such that all the remaining elements in the array are unique. In other words, you must remove the minimum number of elements so that no number appears more than once in the remaining array.
Formally, let \( n \) be the number of elements in the array and let \( a_1,a_2,\dots,a_n \) be the array. You need to choose a subsequence of elements to remove such that, if \( U \) is the set of remaining elements, then \( |U| = \) (the number of unique elements originally in the array). Equivalently, the answer is \( n - |U| \), which is equal to the sum of \( (\text{frequency}(x) - 1) \) for every distinct element \( x \) appearing in the array.
Input Format: The first line contains a single integer \( n \) representing the number of elements. The second line contains \( n \) space-separated integers representing the elements of the array.
Output Format: Output a single integer representing the minimum number of elements that must be removed to ensure that all remaining elements are unique.
Example:
Input: 6 4 1 2 2 1 3</p>Output: 2
inputFormat
The first line of input contains a single integer \( n \), the number of elements in the array. The second line contains \( n \) space-separated integers representing the array.
outputFormat
Output a single integer, the minimum number of elements to remove so that every remaining element is unique.
## sample6
4 1 2 2 1 3
2