VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-a-repeating-and-a-missing-number/

⇱ Missing and Repeating in an Array - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Missing and Repeating in an Array

Last Updated : 9 Feb, 2026

Given an unsorted array arr[] of size n, containing elements from the range 1 to n, it is known that one number in this range is missing, and another number occurs twice in the array, find both the duplicate number and the missing number.

Examples:

Input: arr[] = [3, 1, 3]
Output: [3, 2]
Explanation: 3 is occurs twice and 2 is missing.

Input: arr[] = [4, 3, 6, 2, 1, 1]
Output: [1, 5]
Explanation: 1 is occurs twice and 5 is missing.

[Approach 1] Using Visited Array - O(n) Time and O(n) Space

The idea is to use a frequency array to keep track of how many times each number appears in the original array. Since we know the numbers should range from 1 to n with each appearing exactly once, any number appearing twice is our repeating number, and any number with zero frequency is our missing number.


Output
3 2

[Approach 2] Using Array Marking - O(n) Time and O(1) Space

The main idea is to use the input array itself for tracking: it negates the value at the index corresponding to each element to mark it as visited. If it encounters a value that has already been negated, it identifies that number as the repeating one. In a second pass, it finds the index that remains positive, which corresponds to the missing number.


Output
3 2

[Approach 3] Making Two Math Equations - O(n) Time and O(1) Space

The idea is to use mathematical equations based on the sum and sum of squares of numbers from 1 to n. The difference between expected and actual sums will give us one equation, and the difference between expected and actual sum of squares will give us another equation. Solving these equations yields our missing and repeating numbers.

Illustration:

👁 Duplicate---missing-Element

Note: This method can cause arithmetic overflow as we calculate the sum of squares (or product) and sum of all array elements.


Output
3 2

An Alternate way to make two equations:

  • Let x be the missing and y be the repeating element.
  • Get the sum of all numbers using formula S = n(n+1)/2 - x + y
  • Get product of all numbers using formula P = 1*2*3*...*n * y / x
  • The above two steps give us two equations, we can solve the equations and get the values of x and y.

[Approach 4] Using XOR - O(n) Time and O(1) Space

The idea is to use XOR operations to isolate the missing and repeating numbers. By XORing all array elements with numbers 1 to n, we get the XOR of our missing and repeating numbers. Then, using a set bit in this XOR result, we can divide all numbers into two groups, which helps us separate the missing and repeating numbers.

Refer to Find the two numbers with odd occurrences in an unsorted array to understand how groups will be created.

Step by step approach:

  • XOR all array elements and numbers from 1 to n to get XOR of missing and repeating numbers.
  • Find the rightmost set bit in this XOR result using xorVal & ~(xorVal-1).
  • Use this set bit to divide array elements and numbers 1 to n into two groups.
  • XOR elements of first group to get x and second group to get y.
  • Count occurrences of x in original array to determine which is missing and which is repeating.
  • If x appears in array, x is repeating and y is missing; otherwise vice versa.
  • Return both the repeating and missing numbers.

Output
3 2
Comment