Find the duplicate values in a list
Coding problemEasyAsked in: Accenture, Capgemini, Infosys, TCS
Given a list of integers, return every value that appears more than once. List each such value only once, in the order of its first appearance in the list.
If no value repeats, return an empty list. The largest hidden tests have 300,000 values, so comparing every pair is too slow.
Examples
Example 1
Input: nums = [4,3,2,7,8,2,3,1]
Output: [3,2]
3 first appears before 2.
Example 2
Input: nums = [1,2,3]
Output: []
Example 3
Input: nums = [5,5,5]
Output: [5]
Constraints
- The list holds between 0 and 300,000 integers
- Each value is a 32-bit integer
Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.
Run your code to see the results here. Ctrl or Cmd + Enter runs; add Shift to submit.