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
Returns (int) 2

Example 2

Input
nums (int[]) = [1,1,2]
size (int) = 3
Returns (int) 1

Example 3

Input
nums (int[]) = [1,2,3]
size (int) = 3
Returns (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
Ready to test your solutionRun Code checks your solution against the test cases.Shift + Enter to run

Notifications

Updates are enabled for this browser.

Receive new tutorials, articles and learning resources. You can turn updates off anytime.

Test browser code

Use this code in admin to send a test notification to this browser.