Duplicate Element Identification — Problem Statement & Solution Guide

ArraysMediumCyclic Sort / Index Hashing
TimeO(n)
|
SpaceO(1)

Quick Answer & Algorithm Key Takeaway

Master coding challenges related to Arrays and solve the Duplicate Element Identification problem optimally.

TopicArrays
PatternCyclic Sort / Index Hashing
TimeO(n)
SpaceO(1)

Problem Description

Given an integer array nums of length n where every element satisfies 1 ≤ nums[i] ≤ n, each value appears either once or exactly twice. Identify all values that occur twice and return them in any order. The algorithm must run in linear time O(n) and may only use constant extra space besides the output list.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Duplicate Element Identification"

medium

WHY DOES IT MATTER?

Detecting duplicates in linear time with constant extra memory is a classic constraint that appears in memory‑constrained environments, embedded systems, and interview settings where space efficiency is a differentiator.

OPTIMIZATION CHALLENGE

The breakthrough is realizing that the input array’s indices provide a free hash space; by encoding visitation state directly in the element at the mapped index (via sign flip or additive offset), we eliminate the need for auxiliary data structures.

REAL-WORLD CONNECTION

In distributed log aggregation, each log entry carries a sequence ID within a bounded range. Identifying retransmitted IDs without allocating a separate hash table mirrors this in‑place duplicate detection, reducing memory pressure on the aggregation node.

During the interview, walk through a concrete example on the whiteboard, show the sign‑flip step for each element, and explicitly state that you restore the array only if the problem demands it – this demonstrates both correctness and awareness of side effects.

COMPLEXITY AT A GLANCE

⏱ Time:O(n)
💾 Space:O(1)

Core Theory — Why This Approach?

The problem leverages the fact that the input array length n also bounds the value range (1 ≤ nums[i] ≤ n). This creates a perfect one‑to‑one mapping between values and valid indices (value v maps to index v‑1). A naïve solution would compare each element with every other, yielding O(n²) time, which quickly becomes infeasible for large n. By treating the array itself as a hash table, we can encode visitation information directly in the input without extra storage: when we encounter a value v we flip the sign of the element at index v‑1 (or add n) to mark that v has been seen. If we encounter v again and the marker is already set, we have identified a duplicate. This in‑place marking respects the constant‑space constraint while preserving linear traversal, delivering the optimal O(n) time and O(1) auxiliary space solution.

Interview Questions on This Problem

Q1How would you modify the algorithm if the numbers could appear up to three times instead of twice?

You can keep a count encoded in the sign and magnitude: add n to the indexed slot on first visit, add another n on second visit, and when the value exceeds 2n you know the element appeared three times. Finally, collect indices whose slot value is >2n.

Q2Why is it safe to mutate the input array in this problem, and what would you do if the array must remain unchanged?

The problem statement permits constant extra space besides the output, so mutating the array is allowed. If the array must stay immutable, you can copy it (O(n) extra space) or use a bit‑set of size n (still O(n) space) to track occurrences.

Q3Explain how the same technique can be used to find all missing numbers in the range [1,n] when each appears once or not at all.

After the same sign‑flipping pass, any index i whose element remains positive indicates that number i+1 never appeared, because its marker was never toggled. Collect those i+1 values as the missing numbers.

Examples

Example 1

Input

[3,4,1,4,2]

Output

[4]

Explanation: Traverse the array, marking visited indices by negating the element at the index equal to the current value minus one. When a value points to an already‑negative element, that value has been seen before and is added to the result. After processing, only the number 4 causes a second visit, so the output is [4].

Example 2

Input

[5,1,5,2,3,2]

Output

[5,2]

Explanation: Processing each element with the same marking technique reveals that 5 and 2 are the only numbers whose corresponding positions become negative twice, indicating they each appear twice. The result can be returned in any order, e.g., [5,2].

Example 3

Input

[1,2,3,4,5]

Output

[]

Explanation: Each index is visited exactly once; no position is encountered a second time, so no duplicate values exist. The algorithm therefore returns an empty list.

Constraints

  • 1 <= nums.length <= 100000
  • 1 <= nums[i] <= nums.length
  • Each element appears either once or twice

Optimal Approach & Strategy

Iterate once, using each element’s absolute value as an index and flip the sign of the element at that index; a second negative sign indicates a duplicate, achieving O(n) time and O(1) extra space.

Brute Force Approach

Use two nested loops to compare every pair of elements and collect values that match, which runs in O(n²) time.

Code Solutions

JavaScript Solution
Time: O(n)
var findDuplicates = function(nums) {
    const duplicates = [];
    
    for (let i = 0; i < nums.length; i++) {
        const index = Math.abs(nums[i]) - 1;
        
        // If the value at the index is already negative, it means we've seen this number before
        if (nums[index] < 0) {
            duplicates.push(Math.abs(nums[i]));
        } else {
            // Mark the index as visited by making the value negative
            nums[index] = -nums[index];
        }
    }
    
    return duplicates;
};

// Example usage
const nums = [3, 4, 1, 4, 2];
console.log(findDuplicates(nums));

Asked in Top Tech Interviews

Infosys

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.