# 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 duplicates entries and you do not know what N is. With only 4 Kilobytes of memory available, how would print all duplicates 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
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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.

## C++

 `// C++ program to print all Duplicates in array ` `#include ` `using` `namespace` `std; ` ` `  `// A class to represent array of bits using ` `// array of integers ` `class` `BitArray ` `{ ` `    ``int` `*arr; ` ` `  `    ``public``: ` `    ``BitArray() {} ` ` `  `    ``// Constructor ` `    ``BitArray(``int` `n) ` `    ``{ ` `        ``// Devide by 32. To store n bits, we need ` `        ``// n/32 + 1 integers (Assuming int is stored ` `        ``// using 32 bits) ` `        ``arr = ``new` `int``[(n >> 5) + 1]; ` `    ``} ` ` `  `    ``// Get value of a bit at given position ` `    ``bool` `get(``int` `pos) ` `    ``{ ` `        ``// Divide by 32 to find position of ` `        ``// integer. ` `        ``int` `index = (pos >> 5); ` ` `  `        ``// Now find bit number in arr[index] ` `        ``int` `bitNo = (pos & 0x1F); ` ` `  `        ``// Find value of given bit number in ` `        ``// arr[index] ` `        ``return` `(arr[index] & (1 << bitNo)) != 0; ` `    ``} ` ` `  `    ``// Sets a bit at given position ` `    ``void` `set(``int` `pos) ` `    ``{ ` `        ``// Find index of bit position ` `        ``int` `index = (pos >> 5); ` ` `  `        ``// Set bit number in arr[index] ` `        ``int` `bitNo = (pos & 0x1F); ` `        ``arr[index] |= (1 << bitNo); ` `    ``} ` ` `  `    ``// Main function to print all Duplicates ` `    ``void` `checkDuplicates(``int` `arr[], ``int` `n) ` `    ``{ ` `        ``// create a bit with 32000 bits ` `        ``BitArray ba = BitArray(320000); ` ` `  `        ``// Traverse array elements ` `        ``for` `(``int` `i = 0; i < n; i++) ` `        ``{ ` `            ``// Index in bit array ` `            ``int` `num = arr[i]; ` ` `  `            ``// If num is already present in bit array ` `            ``if` `(ba.get(num)) ` `                ``cout << num << ``" "``; ` ` `  `            ``// Else insert num ` `            ``else` `                ``ba.set(num); ` `        ``} ` `    ``} ` `}; ` ` `  `// Driver code ` `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; ` `} ` ` `  `// This code is contributed by ` `// sanjeev2552 `

## Java

 `// Java program to print all Duplicates in array ` `import` `java.util.*; ` `import` `java.lang.*; ` `import` `java.io.*; ` ` `  `// A class to represent array of bits using ` `// array of integers ` `class` `BitArray ` `{ ` `    ``int``[] arr; ` ` `  `    ``// Constructor ` `    ``public` `BitArray(``int` `n) ` `    ``{ ` `        ``// Devide by 32. To store n bits, we need ` `        ``// n/32 + 1 integers (Assuming int is stored ` `        ``// using 32 bits) ` `        ``arr = ``new` `int``[(n>>``5``) + ``1``]; ` `    ``} ` ` `  `    ``// Get value of a bit at given position ` `    ``boolean` `get(``int` `pos) ` `    ``{ ` `        ``// Divide by 32 to find position of ` `        ``// integer. ` `        ``int` `index = (pos >> ``5``); ` ` `  `        ``// Now find bit number in arr[index] ` `        ``int` `bitNo  = (pos & ``0x1F``); ` ` `  `        ``// Find value of given bit number in ` `        ``// arr[index] ` `        ``return` `(arr[index] & (``1` `<< bitNo)) != ``0``; ` `    ``} ` ` `  `    ``// Sets a bit at given position ` `    ``void` `set(``int` `pos) ` `    ``{ ` `        ``// Find index of bit position ` `        ``int` `index = (pos >> ``5``); ` ` `  `        ``// Set bit number in arr[index] ` `        ``int` `bitNo = (pos & ``0x1F``); ` `        ``arr[index] |= (``1` `<< bitNo); ` `    ``} ` ` `  `    ``// Main function to print all Duplicates ` `    ``static` `void` `checkDuplicates(``int``[] arr) ` `    ``{ ` `        ``// create a bit with 32000 bits ` `        ``BitArray ba = ``new` `BitArray(``320000``); ` ` `  `        ``// Traverse array elements ` `        ``for` `(``int` `i=``0``; i

## Python3

 `# Python3 program to print all Duplicates in array ` ` `  `# A class to represent array of bits using ` `# array of integers ` `class` `BitArray: ` ` `  `    ``# Constructor ` `    ``def` `__init__(``self``, n): ` ` `  `        ``# Divide by 32. To store n bits, we need ` `        ``# n/32 + 1 integers (Assuming int is stored ` `        ``# using 32 bits) ` `        ``self``.arr ``=` `[``0``] ``*` `((n >> ``5``) ``+` `1``) ` ` `  `    ``# Get value of a bit at given position ` `    ``def` `get(``self``, pos): ` ` `  `        ``# Divide by 32 to find position of ` `        ``# integer. ` `        ``self``.index ``=` `pos >> ``5` ` `  `        ``# Now find bit number in arr[index] ` `        ``self``.bitNo ``=` `pos & ``0x1F` ` `  `        ``# Find value of given bit number in ` `        ``# arr[index] ` `        ``return` `(``self``.arr[``self``.index] &  ` `                   ``(``1` `<< ``self``.bitNo)) !``=` `0` ` `  `    ``# Sets a bit at given position ` `    ``def` `set``(``self``, pos): ` ` `  `        ``# Find index of bit position ` `        ``self``.index ``=` `pos >> ``5` ` `  `        ``# Set bit number in arr[index] ` `        ``self``.bitNo ``=` `pos & ``0x1F` `        ``self``.arr[``self``.index] |``=` `(``1` `<< ``self``.bitNo) ` ` `  `# Main function to print all Duplicates ` `def` `checkDuplicates(arr): ` ` `  `    ``# create a bit with 32000 bits ` `    ``ba ``=` `BitArray(``320000``) ` ` `  `    ``# Traverse array elements ` `    ``for` `i ``in` `range``(``len``(arr)): ` ` `  `        ``# Index in bit array ` `        ``num ``=` `arr[i] ` ` `  `        ``# If num is already present in bit array ` `        ``if` `ba.get(num): ` `            ``print``(num, end ``=` `" "``) ` ` `  `        ``# Else insert num ` `        ``else``: ` `            ``ba.``set``(num) ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `"__main__"``: ` `    ``arr ``=` `[``1``, ``5``, ``1``, ``10``, ``12``, ``10``] ` `    ``checkDuplicates(arr) ` ` `  `# This code is conributed by ` `# sanjeev2552 `

## C#

 `// C# program to print all Duplicates in array ` `  `  `// A class to represent array of bits using  ` `// array of integers  ` `using` `System; ` ` `  `class` `BitArray  ` `{  ` `    ``int``[] arr;  ` ` `  `    ``// Constructor  ` `    ``public` `BitArray(``int` `n)  ` `    ``{  ` `        ``// Devide by 32. To store n bits, we need  ` `        ``// n/32 + 1 integers (Assuming int is stored  ` `        ``// using 32 bits)  ` `        ``arr = ``new` `int``[(``int``)(n >> 5) + 1];  ` `    ``}  ` ` `  `    ``// Get value of a bit at given position  ` `    ``bool` `get``(``int` `pos)  ` `    ``{  ` `        ``// Divide by 32 to find position of  ` `        ``// integer.  ` `        ``int` `index = (pos >> 5);  ` ` `  `        ``// Now find bit number in arr[index]  ` `        ``int` `bitNo = (pos & 0x1F);  ` ` `  `        ``// Find value of given bit number in  ` `        ``// arr[index]  ` `        ``return` `(arr[index] & (1 << bitNo)) != 0;  ` `    ``}  ` ` `  `    ``// Sets a bit at given position  ` `    ``void` `set``(``int` `pos)  ` `    ``{  ` `        ``// Find index of bit position  ` `        ``int` `index = (pos >> 5);  ` ` `  `        ``// Set bit number in arr[index]  ` `        ``int` `bitNo = (pos & 0x1F);  ` `        ``arr[index] |= (1 << bitNo);  ` `    ``}  ` ` `  `    ``// Main function to print all Duplicates  ` `    ``static` `void` `checkDuplicates(``int``[] arr)  ` `    ``{  ` `        ``// create a bit with 32000 bits  ` `        ``BitArray ba = ``new` `BitArray(320000);  ` ` `  `        ``// Traverse array elements  ` `        ``for` `(``int` `i = 0; i < arr.Length; i++)  ` `        ``{  ` `            ``// Index in bit array  ` `            ``int` `num = arr[i];  ` ` `  `            ``// If num is already present in bit array  ` `            ``if` `(ba.``get``(num))  ` `                ``Console.Write(num + ``" "``);  ` ` `  `            ``// Else insert num  ` `            ``else` `                ``ba.``set``(num);  ` `        ``}  ` `    ``}  ` ` `  `    ``// Driver code  ` `    ``public` `static` `void` `Main() ` `    ``{  ` `        ``int``[] arr = {1, 5, 1, 10, 12, 10};  ` `        ``checkDuplicates(arr);  ` `    ``}  ` `}  ` ` `  `// This code is contributed by Rajput-Ji `

Output:

```1 10
```

This article is contributed by Mr. Somesh Awasthi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

Improved By : Rajput-Ji, sanjeev2552

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.