Intermediate Level

Implement the findAllDuplicatesCount method that counts duplicate values found in the array.

The input contains an integer array nums and its length size. Your task is to return how many distinct values appear more than once.

The answer is the count of duplicate values, not the total number of repeated positions.

For example, in [4,3,2,7,8,2,3,1], the values 2 and 3 are duplicates, so the answer is 2.

Example 1
Input:
nums (int[]) = [4,3,2,7,8,2,3,1]
size (int) = 8
Return:
(int) 2
Example 2
Input:
nums (int[]) = [1,1,2]
size (int) = 3
Return:
(int) 1
Example 3
Input:
nums (int[]) = [1,2,3]
size (int) = 3
Return:
(int) 0

Count the frequency of every number using a map.

After all frequencies are known, scan the map and count how many values have a frequency greater than 1.

This works even when a number appears more than twice because it is counted only once as a duplicate value.

Pseudocode:

function findAllDuplicatesCount(nums, size):
    create empty map frequency
    for each number in nums:
        frequency[number] = frequency[number] + 1
    count = 0
    for each value in frequency:
        if frequency[value] > 1:
            count++
    return count
Run your code to see the result.