
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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
// 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
- Related Articles
- Find Duplicates of array using bit array in Python
- Find All Duplicates in an Array in C++
- How to find duplicates in an array using set() and filter() methods in JavaScript?
- Removing consecutive duplicates from strings in an array using JavaScript
- Counting duplicates and aggregating array of objects in JavaScript
- Merge and remove duplicates in JavaScript Array
- Remove Duplicates from Sorted Array in Python
- Find a Fixed Point in an array with duplicates allowed in C++
- Remove duplicates from a array of objects JavaScript
- Remove array duplicates by property - JavaScript
- Check for duplicates in an array in MongoDB?
- Commons including duplicates in array elements in JavaScript
- How to remove duplicates from the sorted array and return the non-duplicated array using C#?
- Check duplicates of certain field for inner array in MongoDB
- Removing duplicates from a sorted array of literals in JavaScript
