Skip to main content
Back to problems
Leetcode
Medium
Arrays
Bit Manipulation
Find All Duplicates In An Array

Find every value that appears exactly twice in an array of integers.

Acceptance 0%
Problem Statement

Problem

Given an integer array nums of length n where each value is in the range 1 to n, return all numbers that appear exactly twice.

You should aim for an algorithm that runs in linear time and uses only constant extra space, excluding the output array.

Notes

  • A number may appear once or twice, but not more than twice.
  • The order of the returned duplicates does not matter.

Input Format

  • An integer array nums.
  • nums.length = n.
  • Each nums[i] is an integer in the range 1..n.

Output Format

  • Return an array containing all values that appear exactly twice in nums.
  • The order of elements in the output does not matter.

Constraints

  • 1n1051 \le n \le 10^5
  • 1nums[i]n1 \le nums[i] \le n
  • Each element appears once or twice.
  • Aim for O(n)O(n) time and O(1)O(1) extra space, excluding output storage.
Examples
Sample cases returned by the problem API.

Example 1

Input

nums = [4,3,2,7,8,2,3,1]

Output

[2,3]

Explanation

Both 2 and 3 appear exactly twice. The order of the answer is not important.

Example 2

Input

nums = [1,1,2]

Output

[1]

Explanation

Only 1 appears twice.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.