Find Duplicates of array using bit array
You have an array of N numbers, where N is at most 32,000. The array may have duplicate entries and you do not know what N is. With only 4 Kilobytes of memory available, how would print all duplicate elements in the array ?.
Examples:
Input : arr[] = {1, 5, 1, 10, 12, 10}
Output : 1 10
1 and 10 appear more than once in given
array.
Input : arr[] = {50, 40, 50}
Output : 50
Asked In: Amazon
We have 4 Kilobytes of memory which means we can address up to 8 * 4 * 210 bits. Note that 32 * 210 bits is greater than 32000. We can create a bit with 32000 bits, where each bit represents one integer. Note: If you need to create a bit with more than 32000 bits, then you can create easily more and more than 32000; Using this bit vector, we can then iterate through the array, flagging each element v by setting bit v to 1. When we come across a duplicate element, we print it. Below is the implementation of the idea.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
class BitArray
{
int *arr;
public :
BitArray() {}
BitArray( int n)
{
arr = new int [(n >> 5) + 1];
}
bool get( int pos)
{
int index = (pos >> 5);
int bitNo = (pos & 0x1F);
return (arr[index] & (1 << bitNo)) != 0;
}
void set( int pos)
{
int index = (pos >> 5);
int bitNo = (pos & 0x1F);
arr[index] |= (1 << bitNo);
}
void checkDuplicates( int arr[], int n)
{
BitArray ba = BitArray(320000);
for ( int i = 0; i < n; i++)
{
int num = arr[i];
if (ba.get(num))
cout << num << " " ;
else
ba.set(num);
}
}
};
int main()
{
int arr[] = {1, 5, 1, 10, 12, 10};
int n = sizeof (arr) / sizeof (arr[0]);
BitArray obj = BitArray();
obj.checkDuplicates(arr, n);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
public class BitArray
{
int [] arr;
public BitArray( int n)
{
arr = new int [(n>> 5 ) + 1 ];
}
boolean get( int pos)
{
int index = (pos >> 5 );
int bitNo = (pos & 0x1F );
return (arr[index] & ( 1 << bitNo)) != 0 ;
}
void set( int pos)
{
int index = (pos >> 5 );
int bitNo = (pos & 0x1F );
arr[index] |= ( 1 << bitNo);
}
static void checkDuplicates( int [] arr)
{
BitArray ba = new BitArray( 320000 );
for ( int i= 0 ; i<arr.length; i++)
{
int num = arr[i] - 1 ;
if (ba.get(num))
System.out.print(num + " " );
else
ba.set(num);
}
}
public static void main(String[] args) throws
java.lang.Exception
{
int [] arr = { 1 , 5 , 1 , 10 , 12 , 10 };
checkDuplicates(arr);
}
}
|
Python3
class BitArray:
def __init__( self , n):
self .arr = [ 0 ] * ((n >> 5 ) + 1 )
def get( self , pos):
self .index = pos >> 5
self .bitNo = pos & 0x1F
return ( self .arr[ self .index] &
( 1 << self .bitNo)) ! = 0
def set ( self , pos):
self .index = pos >> 5
self .bitNo = pos & 0x1F
self .arr[ self .index] | = ( 1 << self .bitNo)
def checkDuplicates(arr):
ba = BitArray( 320000 )
for i in range ( len (arr)):
num = arr[i]
if ba.get(num):
print (num, end = " " )
else :
ba. set (num)
if __name__ = = "__main__" :
arr = [ 1 , 5 , 1 , 10 , 12 , 10 ]
checkDuplicates(arr)
|
C#
using System;
class BitArray
{
int [] arr;
public BitArray( int n)
{
arr = new int [( int )(n >> 5) + 1];
}
bool get ( int pos)
{
int index = (pos >> 5);
int bitNo = (pos & 0x1F);
return (arr[index] & (1 << bitNo)) != 0;
}
void set ( int pos)
{
int index = (pos >> 5);
int bitNo = (pos & 0x1F);
arr[index] |= (1 << bitNo);
}
static void checkDuplicates( int [] arr)
{
BitArray ba = new BitArray(320000);
for ( int i = 0; i < arr.Length; i++)
{
int num = arr[i];
if (ba. get (num))
Console.Write(num + " " );
else
ba. set (num);
}
}
public static void Main()
{
int [] arr = {1, 5, 1, 10, 12, 10};
checkDuplicates(arr);
}
}
|
Javascript
class BitArray {
constructor(n){
this .arr = new Array((n >> 5) + 1);
}
get(pos){
let index = (pos >> 5);
let bitNo = (pos & 0x1F);
let arrCopy = this .arr
return (arrCopy[index] & (1 << bitNo)) != 0;
}
set(pos){
var index = (pos >> 5);
var bitNo = (pos & 0x1F);
var arr1 = this .arr;
arr1[index] = arr1[index] | (1 << bitNo);
this .arr = arr1;
}
}
function checkDuplicates(arr){
var ba = new BitArray(320000);
for ( var i = 0; i < arr.length; i++) {
var num = arr[i];
if (ba.get(num))
console.log(num);
else
ba.set(num);
}
}
var a = [ 1, 5, 1, 10, 12, 10 ];
checkDuplicates(a);
|
Time Complexity: O(N)
Space Complexity: O(1)
Last Updated :
23 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...