218. Find All Duplicates in an Array

Medium
Index Sign Marking
Array

Problem

Given an integer array nums of length n where each value is between 1 and n and each value appears once or twice, return all values that appear twice.

Examples

Example 1

Input: [4,3,2,7,8,2,3,1]
Output: [2,3]

Example 2

Input: [1,1,2]
Output: [1]

Example 3

Input: [1]
Output: []
Constraints

1 <= nums.length <= 100000

1 <= nums[i] <= nums.length

Each value appears once or twice.

Hints

?? Use abs(nums[i]) - 1 as an index.

?? A previously marked index reveals a duplicate.

Expected Complexity
Time: O(n)
Space: O(1) auxiliary
Follow-up

Can you solve it in O(n) time with O(1) auxiliary space?

Practice Notes

Attempts: 0

Time spent: 0min

Hints used: 0

00:00
Loading...
Case 1
Case 2
Case 3
Input
[4,3,2,7,8,2,3,1]
Expected
[2,3]