Medium
Find All Duplicates in an Array
- Time limit
- 2 s
- Memory limit
- 256 MB
- Estimated
- 20 min
- Array Techniques
Description
Description
Given an integer array nums of length n where every value lies in the range [1, n] and each value appears either once or exactly twice, return a list of every value that appears twice. The returned values must be sorted in increasing order.
Examples
Example 1
- Input:
nums = [4, 3, 2, 7, 8, 2, 3, 1] - Output:
[2, 3]
Example 2
- Input:
nums = [1, 1, 2] - Output:
[1]
Example 3
- Input:
nums = [1] - Output:
[]
Expected Function Behavior
Implement findDuplicates(nums) and return the sorted list of values that appear twice.
Constraints
1 <= n == nums.length <= 1000001 <= nums[i] <= n- Each value in
[1, n]appears either once or exactly twice.
Sample cases
3Sample 1
Input
nums = [4,3,2,7,8,2,3,1]
Expected output
[2,3]
Sample 2
Input
nums = [1,1,2]
Expected output
[1]
Skills and languages
Patterns
- Cyclic Sort and Index as Hash
Languages
- Python 3
Solve this problem
Create a free account to write, run and submit your solution.
- Write Python or Java in the browser, with your draft saved as you type
- Run your code on the examples as often as you like
- Submit to a sandboxed judge and see exactly which case failed
- Ask the AI mentor for a hint without being handed the answer