Find all duplicate and missing numbers in given permutation array of 1 to N
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:
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 numbers.
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++
#include <bits/stdc++.h>
using namespace std;
void findElements( int arr[], int N)
{
int i = 0;
vector< int > missing;
set< int > duplicate;
set< int >::iterator it;
while (i != N) {
cout << i << " # " ;
if (arr[i] != arr[arr[i] - 1]) {
swap(arr[i], arr[arr[i] - 1]);
}
else {
i++;
}
}
for (i = 0; i < N; i++) {
if (arr[i] != i + 1) {
missing.push_back(i + 1);
duplicate.insert(arr[i]);
}
}
cout << "Missing Numbers: " ;
for ( auto & it : missing)
cout << it << ' ' ;
cout << "\nDuplicate Numbers: " ;
for ( auto & it : duplicate)
cout << it << ' ' ;
}
int main()
{
int arr[] = { 1, 2, 2, 2, 4, 5, 7 };
int N = sizeof (arr) / sizeof (arr[0]);
findElements(arr, N);
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.HashSet;
class GFG {
static void findElements( int arr[], int N) {
int i = 0 ;
ArrayList<Integer> missing = new ArrayList<Integer>();
HashSet<Integer> duplicate = new HashSet<Integer>();
while (i != N) {
if (arr[i] != arr[arr[i] - 1 ]) {
int temp = arr[i];
arr[i] = arr[arr[i] - 1 ];
arr[temp - 1 ] = temp;
}
else {
i++;
}
}
for (i = 0 ; i < N; i++) {
if (arr[i] != i + 1 ) {
missing.add(i + 1 );
duplicate.add(arr[i]);
}
}
System.out.print( "Missing Numbers: " );
for (Integer itr : missing)
System.out.print(itr + " " );
System.out.print( "\nDuplicate Numbers: " );
for (Integer itr : duplicate)
System.out.print(itr + " " );
}
public static void main(String args[]) {
int arr[] = { 1 , 2 , 2 , 2 , 4 , 5 , 7 };
int N = arr.length;
findElements(arr, N);
}
}
|
Python3
def findElements(arr, N) :
i = 0 ;
missing = [];
duplicate = set ();
while (i ! = N) :
if (arr[i] ! = arr[arr[i] - 1 ]) :
t = arr[i]
arr[i] = arr[arr[i] - 1 ]
arr[t - 1 ] = t
else :
i + = 1 ;
for i in range (N) :
if (arr[i] ! = i + 1 ) :
missing.append(i + 1 );
duplicate.add(arr[i]);
print ( "Missing Numbers: " ,end = "");
for it in missing:
print (it,end = " " );
print ( "\nDuplicate Numbers: " ,end = "");
for it in list (duplicate) :
print (it, end = ' ' );
if __name__ = = "__main__" :
arr = [ 1 , 2 , 2 , 2 , 4 , 5 , 7 ];
N = len (arr);
findElements(arr, N);
|
C#
using System;
using System.Collections.Generic;
class GFG {
static void findElements( int [] arr, int N)
{
int i = 0;
List< int > missing = new List< int >();
HashSet< int > duplicate = new HashSet< int >();
while (i != N) {
if (arr[i] != arr[arr[i] - 1]) {
int temp = arr[i];
arr[i] = arr[arr[i] - 1];
arr[temp - 1] = temp;
}
else {
i++;
}
}
for (i = 0; i < N; i++) {
if (arr[i] != i + 1) {
missing.Add(i + 1);
duplicate.Add(arr[i]);
}
}
Console.Write( "Missing Numbers: " );
foreach ( int itr in missing)
Console.Write(itr + " " );
Console.Write( "\nDuplicate Numbers: " );
foreach ( int itr in duplicate)
Console.Write(itr + " " );
}
public static void Main()
{
int [] arr = { 1, 2, 2, 2, 4, 5, 7 };
int N = arr.Length;
findElements(arr, N);
}
}
|
Javascript
<script>
function findElements(arr, N) {
let i = 0;
let missing = [];
let duplicate = new Set();
while (i != N)
{
if (arr[i] != arr[arr[i] - 1]) {
t = arr[i];
arr[i] = arr[arr[i] - 1];
arr[t - 1] = t;
}
else {
i += 1;
}
}
for (let i = 0; i < N; i++)
{
if (arr[i] != i + 1)
{
missing.push(i + 1);
duplicate.add(arr[i]);
}
}
document.write( "Missing Numbers: " );
for (it of missing) document.write(it + " " );
document.write( "<br>Duplicate Numbers: " );
for (let it of [...duplicate]) document.write(it + " " );
}
let arr = [1, 2, 2, 2, 4, 5, 7];
let N = arr.length;
findElements(arr, N);
</script>
|
Output:
Missing Numbers: 3 6
Duplicate Numbers: 2
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
31 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...