#C5215. Minimum Deletions to Achieve Unique Array Elements

    ID: 48840 Type: Default 1000ms 256MiB

Minimum Deletions to Achieve Unique Array Elements

Minimum Deletions to Achieve Unique Array Elements

You are given an array of integers. Your task is to determine the minimum number of deletions required so that all the remaining elements are unique.

In other words, for each element that appears more than once, you need to delete its duplicates until only one copy remains. For an element with frequency \( f \), you will need to delete \( f - 1 \) occurrences.

Example: For the array [2, 3, 2, 2, 2], the number 2 appears 4 times. To leave only one 2, you must remove 3 occurrences. Hence, the answer is 3.

The problem consists of multiple test cases. For each test case, you will be given the size of the array followed by the array elements.

inputFormat

The input is provided via standard input (stdin) and has the following format:

  1. An integer \(T\) denoting the number of test cases.
  2. For each test case:
    • An integer \(N\) representing the number of elements in the array.
    • \(N\) space-separated integers representing the array elements.

For example:

3
5
2 3 2 2 2
3
1 2 2
4
1 2 3 4

outputFormat

For each test case, output a single integer on a new line representing the minimum number of deletions required to make all elements in the corresponding array unique.

For the above sample input, the output should be:

3
1
0
## sample
3
5
2 3 2 2 2
3
1 2 2
4
1 2 3 4
3

1 0

</p>