Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Find all duplicate and missing numbers in given permutation array of 1 to N

  • Difficulty Level : Hard
  • Last Updated : 22 Oct, 2021

Given an array arr[] of size N consisting of the first N natural numbers, the task is to find all the repeating and missing numbers over the range [1, N] in the given array.

Examples:

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

Input: arr[] = {1, 1, 2, 3, 3, 5}
Output: 
Missing Numbers: [4, 6]
Duplicate Numbers: [1, 3]
Explanation:
As 4 and 6 are not in arr[] Therefore they are missing and 1 is repeating two times and 3 is repeating two times so they are duplicate numbres. 

Input: arr[] = {1, 2, 2, 2, 4, 5, 7}
Output:
Missing Numbers: [3, 6]
Duplicate Numbers: [2]



Approach: The given problem can be solved using the idea discussed in this article where only one element is repeating and the other is duplicate. Follow the steps below to solve the given problem:

  • Initialize an array, say missing[] that stores the missing elements.
  • Initialize a set, say duplicate that stores the duplicate elements.
  • Traverse the given array arr[] using the variable i and perform the following steps:
    • If the value of arr[i] != arr[arr[i] – 1] is true, then the current element is not equal to the place where it is supposed to be if all numbers were present from 1 to N. So swap arr[i] and arr[arr[i] – 1].
    • Otherwise, it means the same element is present at arr[arr[i] – 1].
  • Traverse the given array arr[] using the variable i and if the value of arr[i] is not the same as (i + 1) then the missing element is (i + 1) and the duplicate element is arr[i].
  • After completing the above steps, print the elements stored in missing[] and duplicate[] as the result.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the duplicate and
// the missing elements over the range
// [1, N]
void findElements(int arr[], int N)
{
    int i = 0;
 
    // Stores the missing and duplicate
    // numbers in the array arr[]
    vector<int> missing;
    set<int> duplicate;
 
    // Making an iterator for set
    set<int>::iterator it;
 
    // Traverse the given array arr[]
    while (i != N) {
        cout << i << " # " ;
        // Check if the current element
        // is not same as the element at
        // index arr[i] - 1, then swap
        if (arr[i] != arr[arr[i] - 1]) {
            swap(arr[i], arr[arr[i] - 1]);
        }
 
        // Otherwise, increment the index
        else {
            i++;
        }
    }
 
    // Traverse the array again
    for (i = 0; i < N; i++) {
 
        // If the element is not at its
        // correct position
        if (arr[i] != i + 1) {
 
            // Stores the missing and the
            // duplicate elements
            missing.push_back(i + 1);
            duplicate.insert(arr[i]);
        }
    }
 
    // Print the Missing Number
    cout << "Missing Numbers: ";
    for (auto& it : missing)
        cout << it << ' ';
 
    // Print the Duplicate Number
    cout << "\nDuplicate Numbers: ";
    for (auto& it : duplicate)
        cout << it << ' ';
}
 
// Driver code
int main()
{
    int arr[] = { 1, 2, 2, 2, 4, 5, 7 };
    int N = sizeof(arr) / sizeof(arr[0]);
    findElements(arr, N);
 
    return 0;
}

Python3




# Python3 program for the above approach
 
# Function to find the duplicate and
# the missing elements over the range
# [1, N]
def findElements(arr, N) :
    i = 0;
 
    # Stores the missing and duplicate
    # numbers in the array arr[]
    missing = [];
    duplicate = set();
 
    # Traverse the given array arr[]
    while (i != N) :
         
        # Check if the current element
        # is not same as the element at
        # index arr[i] - 1, then swap
        if (arr[i] != arr[arr[i] - 1]) :
             
            t = arr[i]
            arr[i] = arr[arr[i] - 1]
            arr[t - 1= t
             
        # Otherwise, increment the index
        else :
            i += 1;
 
    # Traverse the array again
    for i in range(N) :
         
        # If the element is not at its
        # correct position
        if (arr[i] != i + 1) :
 
            # Stores the missing and the
            # duplicate elements
            missing.append(i + 1);
            duplicate.add(arr[i]);
 
    # Print the Missing Number
    print("Missing Numbers: ",end="");
     
    for it in missing:
        print(it,end=" ");
 
    # Print the Duplicate Number
    print("\nDuplicate Numbers: ",end="");
     
    for it in list(duplicate) :
        print(it, end=' ');
 
# Driver code
if __name__ == "__main__" :
 
    arr = [ 1, 2, 2, 2, 4, 5, 7 ];
    N = len(arr);
    findElements(arr, N);
 
    # This code is contributed by AnkThon

Javascript




<script>
// Javascript program for the above approach
 
// Function to find the duplicate and
// the missing elements over the range
// [1, N]
function findElements(arr, N) {
  let i = 0;
 
  // Stores the missing and duplicate
  // numbers in the array arr[]
  let missing = [];
  let duplicate = new Set();
 
  // Traverse the given array arr[]
  while (i != N)
  {
   
    // Check if the current element
    // is not same as the element at
    // index arr[i] - 1, then swap
    if (arr[i] != arr[arr[i] - 1]) {
      t = arr[i];
      arr[i] = arr[arr[i] - 1];
      arr[t - 1] = t;
    }
 
    // Otherwise, increment the index
    else {
      i += 1;
    }
  }
 
  // Traverse the array again
  for (let i = 0; i < N; i++)
  {
   
    // If the element is not at its
    // correct position
    if (arr[i] != i + 1)
    {
     
      // Stores the missing and the
      // duplicate elements
      missing.push(i + 1);
      duplicate.add(arr[i]);
    }
  }
 
  // Print the Missing Number
  document.write("Missing Numbers: ");
 
  for (it of missing) document.write(it + " ");
 
  // Print the Duplicate Number
  document.write("<br>Duplicate Numbers: ");
 
  for (let it of [...duplicate]) document.write(it + " ");
}
 
// Driver code
 
let arr = [1, 2, 2, 2, 4, 5, 7];
let N = arr.length;
findElements(arr, N);
 
// This code is contributed by _saurabh_jaiswal
 
</script>
Output: 
Missing Numbers: 3 6 
Duplicate Numbers: 2

 

Time Complexity: O(N)
Auxiliary Space: O(N)




My Personal Notes arrow_drop_up
Recommended Articles
Page :