Find Duplicates of array using bit array in C++


Concept

We have an array of n numbers, where n is maximum 32,000. Now the given array may have duplicate entries and we do not know what n is. Now the question is arisen that with only4 Kilobytes of memory available, how would display or print all duplicates elements in the array?

Input

arr[] = {2, 6, 2, 11, 13, 11}

Output

2 11
2 and 11 appear more than once in given array.

Input

arr[] = {60, 50, 60}

Output

60

Methods

Now we have 4 Kilobytes of memory which indicates we can address up to 8 * 4 * 210 bits.It should be noted that 32 * 210 bits is larger than 32000. So we can generate a bit with32000 bits, where each bit represents one integer.

Again it should be noted that if we require generating a bit with more than 32000 bits then we can generate easily more and more than 32000; implementing this bit vector, we can be able to iterate through the array, flagging each element v by setting bit v to 1. In this case, when we traverse a duplicate element, we print it.

Example

 Live Demo

// C++ program to print all Duplicates in array
#include <bits/stdc++.h>
using namespace std;
// Shows a class to represent an array of bits using
// array of integers
class BitArray{
   int *arr1;
   public:
   BitArray() {}
   // Constructor
   BitArray(int n1){
      // Used to divide by 32. To store n bits, we require
      // n/32 + 1 integers (Assuming int is stored
      // using 32 bits)
      arr1 = new int[(n1 >> 5) + 1];
   }
   // Now get value of a bit at given position
   bool get(int pos1){
      // Used to divide by 32 to find position of
      // integer.
      int index1 = (pos1 >> 5);
      // Now determine bit number in arr[index]
      int bitNo1 = (pos1 & 0x1F);
      // Determine value of given bit number in
      // arr1[index1]
      return (arr1[index1] & (1 << bitNo1)) != 0;
   }
   // Used to set a bit at given position
   void set(int pos1){
      // Determine index of bit position
      int index1 = (pos1 >> 5);
      // Used to set bit number in arr1[index1]
      int bitNo1 = (pos1 & 0x1F);
      arr1[index1] |= (1 << bitNo1);
   }
   //Shows main function to print all Duplicates
   void checkDuplicates1(int arr1[], int n1){
      // Used to create a bit with 32000 bits
      BitArray ba1 = BitArray(320000);
      // Used to traverse array elements
      for (int i = 0; i < n1; i++){
         // Shows index in bit array
         int num1 = arr1[i];
         // Now if num is already present in bit array
         if (ba1.get(num1))
            cout << num1 << " ";
         // Otherwise or else insert num
         else
            ba1.set(num1);
      }
   }
};
// Driver code
int main(){
   int arr1[] = {2, 6, 2, 11, 13, 11};
   int n1 = sizeof(arr1) / sizeof(arr1[0]);
   BitArray obj1 = BitArray();
   obj1.checkDuplicates1(arr1, n1);
   return 0;
}

Output

2 11

Updated on: 24-Jul-2020

232 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements