Find duplicates in constant array with elements 0 to N-1 in O(1) space
Last Updated :
28 Feb, 2023
Given a constant array of n elements which contains elements from 1 to n-1, with any of these numbers appearing any number of times. Find any one of these repeating numbers in O(n) and using only constant memory space.
Examples:
Input : arr[] = {1, 2, 3, 4, 5, 6, 3}
Output : 3
As the given array is constant methods given in below articles will not work.
- Find duplicates in O(n) time and O(1) extra space | Set 1
- Duplicates in an array in O(n) and by using O(1) extra space | Set-2
- We are taking two variables i & j starting from 0
- We will run loop until i reached last elem or found repeated elem
- We will pre-increment the j value so that we can compare elem with next elem
- If we don’t find elem, we will increase i as j will be pointing last elem and then reposition j with
Implementation:
C++
#include <iostream>
using namespace std;
int findduplicate( int a[], int n)
{
int i = 0, j = 0;
while (i < n) {
if (a[i] == a[++j])
return a[j];
if (j == n - 1) {
i++;
j = i;
}
}
return -1;
}
int main()
{
int arr[] = { 1, 2, 4, 3, 4, 5, 6, 3 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << findduplicate(arr, n) << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
public class GFG {
static int findduplicate( int []a, int n)
{
int i= 0 ,j= 0 ;
while (i<n){
if (a[i]==a[++j]) return a[j];
if (j==n- 1 ) {
i++;
j=i;
}
}
return - 1 ;
}
public static void main(String args[])
{
int []arr = { 1 , 2 , 4 , 3 , 4 , 5 , 6 , 3 };
int n = arr.length;
System.out.print(findduplicate(arr, n));
}
}
|
Java
import java.io.*;
import java.util.*;
public class GFG {
static int findduplicate( int []arr, int n)
{
if (n <= 1 )
return - 1 ;
int slow = arr[ 0 ];
int fast = arr[arr[ 0 ]];
while (fast != slow)
{
slow = arr[slow];
fast = arr[arr[fast]];
}
fast = 0 ;
while (slow != fast)
{
slow = arr[slow];
fast = arr[fast];
}
return slow;
}
public static void main(String args[])
{
int []arr = { 1 , 2 , 3 , 4 , 5 , 6 , 3 };
int n = arr.length;
System.out.print(findduplicate(arr, n));
}
}
|
Python 3
def findduplicate(arr, n):
if (n < = 1 ):
return - 1
slow = arr[ 0 ]
fast = arr[arr[ 0 ]]
while (fast ! = slow) :
slow = arr[slow]
fast = arr[arr[fast]]
fast = 0
while (slow ! = fast):
slow = arr[slow]
fast = arr[fast]
return slow
if __name__ = = "__main__" :
arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 3 ]
n = len (arr)
print (findduplicate(arr, n))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int findduplicate( int []arr, int n)
{
if (n <= 1)
return -1;
int slow = arr[0];
int fast = arr[arr[0]];
while (fast != slow)
{
slow = arr[slow];
fast = arr[arr[fast]];
}
fast = 0;
while (slow != fast)
{
slow = arr[slow];
fast = arr[fast];
}
return slow;
}
public static void Main()
{
int []arr = {1, 2, 3, 4, 5, 6, 3};
int n = arr.Length;
Console.Write(findduplicate(arr, n));
}
}
|
PHP
<?php
function findduplicate( $arr , $n )
{
if ( $n <= 1)
return -1;
$slow = $arr [0];
$fast = $arr [ $arr [0]];
while ( $fast != $slow )
{
$slow = $arr [ $slow ];
$fast = $arr [ $arr [ $fast ]];
}
$fast = 0;
while ( $slow != $fast )
{
$slow = $arr [ $slow ];
$fast = $arr [ $fast ];
}
return $slow ;
}
$arr = array ( 1, 2, 3, 4, 5, 6, 3 );
$n = sizeof( $arr );
echo findduplicate( $arr , $n );
?>
|
Javascript
<script>
function findduplicate(arr, n)
{
if (n <= 1)
return -1;
let slow = arr[0];
let fast = arr[arr[0]];
while (fast != slow)
{
slow = arr[slow];
fast = arr[arr[fast]];
}
fast = 0;
while (slow != fast)
{
slow = arr[slow];
fast = arr[fast];
}
return slow;
}
let arr = [1, 2, 3, 4, 5, 6, 3];
let n = arr.length;
document.write(findduplicate(arr, n));
</script>
|
Javascript
function findduplicate( a, n)
{
let i = 0, j = 0;
while (i < n) {
if (a[i] == a[++j])
return a[j];
if (j == n - 1) {
i++;
j = i;
}
}
return -1;
}
let arr = [ 1, 2, 4, 3, 4, 5, 6, 3 ];
let n = arr.length;
console.log(findduplicate(arr, n));
|
Complexity Analysis:
- Time Complexity: O(n*n)
- Auxiliary Space: O(1)
Efficient Approach:
We will use the concept that all elements here are between 1 and n-1.
So we will perform these steps to find the Duplicate element
- Consider a pointer ‘p’ which is currently at index 0.
- Run a while loop until the pointer p reaches the value n.
- if the value of a[p] is -1 then increment the pointer by 1 and skip the iteration
- Else,go to the position of the element to which the current pointer is pointing i.e. at index a[p].
- Now if the value at index a[p] i.e. a[a[p]] is -1 then break the loop as the element a[p] is the duplicate one.
- Otherwise store the value of a[a[p]] in a[p] i.e. a[p]=a[a[p]] and put -1 in a[a[p]] i.e. a[a[p]]=-1.
Code:
C++
#include <iostream>
using namespace std;
void find_duplicate( int a[], int n)
{
int p = 0;
while (p != n) {
if (a[p] == -1) {
p++;
}
else {
if (a[a[p] - 1] == -1) {
cout << a[p] << endl;
break ;
}
else {
a[p] = a[a[p] - 1];
a[a[p] - 1] = -1;
}
}
}
}
int main()
{
int a[] = { 1, 2, 4, 3, 4, 5, 6, 3 };
int n = sizeof (a) / sizeof (a[0]);
find_duplicate(a, n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void find_duplicate( int a[], int n)
{
int p = 0 ;
while (p != n) {
if (a[p] == - 1 ) {
p++;
}
else {
if (a[a[p] - 1 ] == - 1 ) {
System.out.println(a[p]);
break ;
}
else {
a[p] = a[a[p] - 1 ];
a[a[p] - 1 ] = - 1 ;
}
}
}
}
public static void main(String[] args)
{
int [] a = { 1 , 2 , 4 , 3 , 4 , 5 , 6 , 3 };
int n = a.length;
find_duplicate(a, n);
}
}
|
Python3
class GFG :
@staticmethod
def find_duplicate( a, n) :
p = 0
while (p ! = n) :
if (a[p] = = - 1 ) :
p + = 1
else :
if (a[a[p] - 1 ] = = - 1 ) :
print (a[p])
break
else :
a[p] = a[a[p] - 1 ]
a[a[p] - 1 ] = - 1
@staticmethod
def main( args) :
a = [ 1 , 2 , 4 , 3 , 4 , 5 , 6 , 3 ]
n = len (a)
GFG.find_duplicate(a, n)
if __name__ = = "__main__" :
GFG.main([])
|
C#
using System;
public class GFG
{
public static void find_duplicate( int [] a, int n)
{
var p = 0;
while (p != n)
{
if (a[p] == -1)
{
p++;
}
else
{
if (a[a[p] - 1] == -1)
{
Console.WriteLine(a[p]);
break ;
}
else
{
a[p] = a[a[p] - 1];
a[a[p] - 1] = -1;
}
}
}
}
public static void Main(String[] args)
{
int [] a = {1, 2, 4, 3, 4, 5, 6, 3};
var n = a.Length;
GFG.find_duplicate(a, n);
}
}
|
Javascript
function find_duplicate( a, n)
{
let p = 0;
while (p != n) {
if (a[p] == -1) {
p++;
}
else {
if (a[a[p] - 1] == -1) {
console.log(a[p]);
break ;
}
else {
a[p] = a[a[p] - 1];
a[a[p] - 1] = -1;
}
}
}
}
let a = [1, 2, 4, 3, 4, 5, 6, 3 ];
let n = a.length;
find_duplicate(a, n);
|
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Space: O(1)
Another Approach:- Use Hashing to find duplicate element.
Implementation:-
C++
#include <bits/stdc++.h>
using namespace std;
int find_duplicate( int a[], int n)
{
unordered_map< int , int > mm;
for ( int i=0;i<n;i++){
mm[a[i]]++;
if (mm[a[i]]>1) return a[i];
}
}
int main()
{
int a[] = { 1, 2, 4, 3, 4, 5, 6, 3 };
int n = sizeof (a) / sizeof (a[0]);
cout<<find_duplicate(a, n);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static int find_duplicate( int [] a, int n)
{
Map<Integer, Integer> mm = new HashMap<>();
for ( int i = 0 ; i < n; i++) {
if (mm.containsKey(a[i])) {
int val = mm.get(a[i]);
mm.put(a[i], val + 1 );
}
else {
mm.put(a[i], 1 );
}
if (mm.get(a[i]) > 1 )
return a[i];
}
return - 1 ;
}
public static void main(String[] args)
{
int [] a = { 1 , 2 , 4 , 3 , 4 , 5 , 6 , 3 };
int n = a.length;
System.out.print(find_duplicate(a, n));
}
}
|
Python3
def find_duplicate(a, n):
mm = {}
for i in range (n):
if a[i] in mm:
mm[a[i]] + = 1
else :
mm[a[i]] = 1
if mm[a[i]] > 1 :
return a[i]
a = [ 1 , 2 , 4 , 3 , 4 , 5 , 6 , 3 ]
n = len (a)
print (find_duplicate(a, n))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int find_duplicate( int [] a, int n)
{
Dictionary< int , int > mm = new Dictionary< int , int >();
for ( int i = 0; i < n; i++)
{
if (mm.ContainsKey(a[i]))
{
var val = mm[a[i]];
mm.Remove(a[i]);
mm.Add(a[i], val + 1);
}
else
{
mm.Add(a[i], 1);
}
if (mm[a[i]] > 1)
return a[i];
}
return -1;
}
static void Main( string [] args)
{
int [] a = { 1, 2, 4, 3, 4, 5, 6, 3 };
int n = a.Length;
Console.Write(find_duplicate(a, n));
}
}
|
Javascript
function findDuplicate(a) {
let frequency = {};
for (let i = 0; i < a.length; i++)
{
if (a[i] in frequency)
{
frequency[a[i]]++;
}
else
{
frequency[a[i]] = 1;
}
if (frequency[a[i]] > 1)
{
return a[i];
}
}
}
let arr = [1, 2, 4, 3, 4, 5, 6, 3];
console.log(findDuplicate(arr));
|
Output:- 4
Time Complexity:- O(N)
Space Complexity:- O(N)
Share your thoughts in the comments
Please Login to comment...