BackmediumArrays TCS

Find All Duplicates Solution

Problem Statement

Given the scan log, return all over-stocked item IDs, which are items that appear more than twice.

Example 1
Input
[1, 2, 3, 4, 4, 4, 4]
Output
[4]

Explanation: Step-by-step: Given the input [1, 2, 3, 4, 4, 4, 4], we first count the occurrences of each item using a frequency map. The frequency map will be {1: 1, 2: 1, 3: 1, 4: 5}. Then, we iterate over the frequency map and add the item ID to the result list if its frequency is greater than 2. In this case, only item 4 appears more than twice, so the output will be [4].

Example 2
Input
[1, 1, 1, 1, 1, 2, 2, 2, 2, 2]
Output
[1, 2]

Explanation: Step-by-step: Given the input [1, 1, 1, 1, 1, 2, 2, 2, 2, 2], we first count the occurrences of each item using a frequency map. The frequency map will be {1: 5, 2: 5}. Then, we iterate over the frequency map and add the item ID to the result list if its frequency is greater than 2. In this case, both items 1 and 2 appear more than twice, so the output will be [1, 2].

Constraints

  • 1 <= n <= 10^5
  • 1 <= arr[i] <= n
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free