# Find any one of the multiple repeating elements in read only array

Given a **read only** array of size ( n+1 ), find one of the multiple repeating elements in the array where the array contains integers only between 1 and n.

Read only array means that the contents of the array can’t be modified.

**Examples:**

Input : n = 5 arr[] = {1, 1, 2, 3, 5, 4} Output : One of the numbers repeated in the array is: 1 Input : n = 10 arr[] = {10, 1, 2, 3, 5, 4, 9, 8, 5, 6, 4} Output : One of the numbers repeated in the array is: 4 OR 5

Since, the size of the array is n+1 and elements ranges from 1 to n then it is confirmed that there will be at least one repeating element.

A **simple solution** is to create a count array and store counts of all elements. As soon as we encounter an element with count more than 1, we return it. This solution works in O(n) time and requires O(n) extra space.

A **space optimized solution** is to break the given range (from 1 to n) into blocks of size equal to sqrt(n). We maintain the count of elements belonging to each block for every block. Now as the size of array is (n+1) and blocks are of size sqrt(n), then there will be one such block whose size will be more than sqrt(n). For the block whose count is greater than sqrt(n), we can use hashing for the elements of this block to find which element appears more than once.

**Explanation**:

The method described above works because of the following two reasons:

- There would always be a block which has count greater than sqrt(n) because of one extra element. Even when one extra element has been added it will occupy a position in one of the blocks only, making that block to be selected.
- The selected block definitely has a repeating element. Consider that i
^{th}block is selected. Size of the block is greater than sqrt(n) (Hence, it is selected) Maximum distinct elements in this block = sqrt(n). Thus, size can be greater than sqrt(n) only if there is a repeating element in range ( i*sqrt(n), (i+1)*sqrt(n) ].

**Note:** The last block formed may or may not have range equal to sqrt(n). Thus, checking if this block has a repeating element will be different than other blocks. However, this difficulty can be overcome from implementation point of view by initialising the selected block with the last block. This is safe because at least one block has to get selected.

**Below is the step by step algorithm to solve this problem**:

- Divide the array in blocks of size sqrt(n).
- Make a count array which stores the count of element for each block.
- Pick up the block which has count more than sqrt(n), setting the last block

as default. - For the elements belonging to the selected block, use the method of hashing(explained in next step) to find the repeating element in that block.
- We can create a hash array of key value pair, where key is the element in the block and value is the count of number of times the given key is appearing. This can be easily implemented using unordered_map in C++ STL.

Below is the implementation of above idea:

## C++

`// C++ program to find one of the repeating ` `// elements in a read only array ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find one of the repeating ` `// elements ` `int` `findRepeatingNumber(` `const` `int` `arr[], ` `int` `n) ` `{ ` ` ` `// Size of blocks except the ` ` ` `// last block is sq ` ` ` `int` `sq = ` `sqrt` `(n); ` ` ` ` ` `// Number of blocks to incorporate 1 to ` ` ` `// n values blocks are numbered from 0 ` ` ` `// to range-1 (both included) ` ` ` `int` `range = (n / sq) + 1; ` ` ` ` ` `// Count array maintains the count for ` ` ` `// all blocks ` ` ` `int` `count[range] = {0}; ` ` ` ` ` `// Traversing the read only array and ` ` ` `// updating count ` ` ` `for` `(` `int` `i = 0; i <= n; i++) ` ` ` `{ ` ` ` `// arr[i] belongs to block number ` ` ` `// (arr[i]-1)/sq i is considered ` ` ` `// to start from 0 ` ` ` `count[(arr[i] - 1) / sq]++; ` ` ` `} ` ` ` ` ` `// The selected_block is set to last ` ` ` `// block by default. Rest of the blocks ` ` ` `// are checked ` ` ` `int` `selected_block = range - 1; ` ` ` `for` `(` `int` `i = 0; i < range - 1; i++) ` ` ` `{ ` ` ` `if` `(count[i] > sq) ` ` ` `{ ` ` ` `selected_block = i; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// after finding block with size > sq ` ` ` `// method of hashing is used to find ` ` ` `// the element repeating in this block ` ` ` `unordered_map<` `int` `, ` `int` `> m; ` ` ` `for` `(` `int` `i = 0; i <= n; i++) ` ` ` `{ ` ` ` `// checks if the element belongs to the ` ` ` `// selected_block ` ` ` `if` `( ((selected_block * sq) < arr[i]) && ` ` ` `(arr[i] <= ((selected_block + 1) * sq))) ` ` ` `{ ` ` ` `m[arr[i]]++; ` ` ` ` ` `// repeating element found ` ` ` `if` `(m[arr[i]] > 1) ` ` ` `return` `arr[i]; ` ` ` `} ` ` ` `} ` ` ` ` ` `// return -1 if no repeating element exists ` ` ` `return` `-1; ` `} ` ` ` `// Driver Program ` `int` `main() ` `{ ` ` ` `// read only array, not to be modified ` ` ` `const` `int` `arr[] = { 1, 1, 2, 3, 5, 4 }; ` ` ` ` ` `// array of size 6(n + 1) having ` ` ` `// elements between 1 and 5 ` ` ` `int` `n = 5; ` ` ` ` ` `cout << ` `"One of the numbers repeated in"` ` ` `" the array is: "` ` ` `<< findRepeatingNumber(arr, n) << endl; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find one of the repeating ` `// elements in a read only array ` `import` `java.io.*; ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to find one of the repeating ` ` ` `// elements ` ` ` `static` `int` `findRepeatingNumber(` `int` `[] arr, ` `int` `n) ` ` ` `{ ` ` ` `// Size of blocks except the ` ` ` `// last block is sq ` ` ` `int` `sq = (` `int` `) Math.sqrt(n); ` ` ` ` ` `// Number of blocks to incorporate 1 to ` ` ` `// n values blocks are numbered from 0 ` ` ` `// to range-1 (both included) ` ` ` `int` `range = (n / sq) + ` `1` `; ` ` ` ` ` `// Count array maintains the count for ` ` ` `// all blocks ` ` ` `int` `[] count = ` `new` `int` `[range]; ` ` ` ` ` `// Traversing the read only array and ` ` ` `// updating count ` ` ` `for` `(` `int` `i = ` `0` `; i <= n; i++) ` ` ` `{ ` ` ` `// arr[i] belongs to block number ` ` ` `// (arr[i]-1)/sq i is considered ` ` ` `// to start from 0 ` ` ` `count[(arr[i] - ` `1` `) / sq]++; ` ` ` `} ` ` ` ` ` `// The selected_block is set to last ` ` ` `// block by default. Rest of the blocks ` ` ` `// are checked ` ` ` `int` `selected_block = range - ` `1` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < range - ` `1` `; i++) ` ` ` `{ ` ` ` `if` `(count[i] > sq) ` ` ` `{ ` ` ` `selected_block = i; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// after finding block with size > sq ` ` ` `// method of hashing is used to find ` ` ` `// the element repeating in this block ` ` ` `HashMap<Integer, Integer> m = ` `new` `HashMap<>(); ` ` ` `for` `(` `int` `i = ` `0` `; i <= n; i++) ` ` ` `{ ` ` ` `// checks if the element belongs to the ` ` ` `// selected_block ` ` ` `if` `( ((selected_block * sq) < arr[i]) && ` ` ` `(arr[i] <= ((selected_block + ` `1` `) * sq))) ` ` ` `{ ` ` ` `m.put(arr[i], ` `1` `); ` ` ` ` ` `// repeating element found ` ` ` `if` `(m.get(arr[i]) == ` `1` `) ` ` ` `return` `arr[i]; ` ` ` `} ` ` ` `} ` ` ` ` ` `// return -1 if no repeating element exists ` ` ` `return` `-` `1` `; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `// read only array, not to be modified ` ` ` `int` `[] arr = { ` `1` `, ` `1` `, ` `2` `, ` `3` `, ` `5` `, ` `4` `}; ` ` ` ` ` `// array of size 6(n + 1) having ` ` ` `// elements between 1 and 5 ` ` ` `int` `n = ` `5` `; ` ` ` ` ` `System.out.println(` `"One of the numbers repeated in the array is: "` `+ ` ` ` `findRepeatingNumber(arr, n)); ` `} ` `} ` ` ` `// This code is contributed by rachana soma ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3program to find one of the repeating ` `# elements in a read only array ` `from` `math ` `import` `sqrt ` ` ` `# Function to find one of the repeating ` `# elements ` `def` `findRepeatingNumber(arr, n): ` ` ` ` ` `# Size of blocks except the ` ` ` `# last block is sq ` ` ` `sq ` `=` `sqrt(n) ` ` ` ` ` `# Number of blocks to incorporate 1 to ` ` ` `# n values blocks are numbered from 0 ` ` ` `# to range-1 (both included) ` ` ` `range__` `=` `int` `((n ` `/` `sq) ` `+` `1` `) ` ` ` ` ` `# Count array maintains the count for ` ` ` `# all blocks ` ` ` `count ` `=` `[` `0` `for` `i ` `in` `range` `(range__)] ` ` ` ` ` `# Traversing the read only array and ` ` ` `# updating count ` ` ` `for` `i ` `in` `range` `(` `0` `, n ` `+` `1` `, ` `1` `): ` ` ` ` ` `# arr[i] belongs to block number ` ` ` `# (arr[i]-1)/sq i is considered ` ` ` `# to start from 0 ` ` ` `count[` `int` `((arr[i] ` `-` `1` `) ` `/` `sq)] ` `+` `=` `1` ` ` ` ` `# The selected_block is set to last ` ` ` `# block by default. Rest of the blocks ` ` ` `# are checked ` ` ` `selected_block ` `=` `range__ ` `-` `1` ` ` `for` `i ` `in` `range` `(` `0` `, range__ ` `-` `1` `, ` `1` `): ` ` ` `if` `(count[i] > sq): ` ` ` `selected_block ` `=` `i ` ` ` `break` ` ` ` ` `# after finding block with size > sq ` ` ` `# method of hashing is used to find ` ` ` `# the element repeating in this block ` ` ` `m ` `=` `{i:` `0` `for` `i ` `in` `range` `(n)} ` ` ` `for` `i ` `in` `range` `(` `0` `, n ` `+` `1` `, ` `1` `): ` ` ` ` ` `# checks if the element belongs ` ` ` `# to the selected_block ` ` ` `if` `(((selected_block ` `*` `sq) < arr[i]) ` `and` ` ` `(arr[i] <` `=` `((selected_block ` `+` `1` `) ` `*` `sq))): ` ` ` `m[arr[i]] ` `+` `=` `1` ` ` ` ` `# repeating element found ` ` ` `if` `(m[arr[i]] > ` `1` `): ` ` ` `return` `arr[i] ` ` ` ` ` `# return -1 if no repeating element exists ` ` ` `return` `-` `1` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `# read only array, not to be modified ` ` ` `arr ` `=` `[` `1` `, ` `1` `, ` `2` `, ` `3` `, ` `5` `, ` `4` `] ` ` ` ` ` `# array of size 6(n + 1) having ` ` ` `# elements between 1 and 5 ` ` ` `n ` `=` `5` ` ` ` ` `print` `(` `"One of the numbers repeated in the array is:"` `, ` ` ` `findRepeatingNumber(arr, n)) ` ` ` `# This code is contributed by ` `# Sahil_shelangia ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find one of the repeating ` `// elements in a read only array ` `using` `System; ` `using` `System.Collections.Generic; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to find one of the repeating ` ` ` `// elements ` ` ` `static` `int` `findRepeatingNumber(` `int` `[] arr, ` `int` `n) ` ` ` `{ ` ` ` `// Size of blocks except the ` ` ` `// last block is sq ` ` ` `int` `sq = (` `int` `) Math.Sqrt(n); ` ` ` ` ` `// Number of blocks to incorporate 1 to ` ` ` `// n values blocks are numbered from 0 ` ` ` `// to range-1 (both included) ` ` ` `int` `range = (n / sq) + 1; ` ` ` ` ` `// Count array maintains the count for ` ` ` `// all blocks ` ` ` `int` `[] count = ` `new` `int` `[range]; ` ` ` ` ` `// Traversing the read only array and ` ` ` `// updating count ` ` ` `for` `(` `int` `i = 0; i <= n; i++) ` ` ` `{ ` ` ` `// arr[i] belongs to block number ` ` ` `// (arr[i]-1)/sq i is considered ` ` ` `// to start from 0 ` ` ` `count[(arr[i] - 1) / sq]++; ` ` ` `} ` ` ` ` ` `// The selected_block is set to last ` ` ` `// block by default. Rest of the blocks ` ` ` `// are checked ` ` ` `int` `selected_block = range - 1; ` ` ` `for` `(` `int` `i = 0; i < range - 1; i++) ` ` ` `{ ` ` ` `if` `(count[i] > sq) ` ` ` `{ ` ` ` `selected_block = i; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// after finding block with size > sq ` ` ` `// method of hashing is used to find ` ` ` `// the element repeating in this block ` ` ` `Dictionary<` `int` `,` `int` `> m = ` `new` `Dictionary<` `int` `,` `int` `>(); ` ` ` `for` `(` `int` `i = 0; i <= n; i++) ` ` ` `{ ` ` ` `// checks if the element belongs to the ` ` ` `// selected_block ` ` ` `if` `( ((selected_block * sq) < arr[i]) && ` ` ` `(arr[i] <= ((selected_block + 1) * sq))) ` ` ` `{ ` ` ` `m.Add(arr[i], 1); ` ` ` ` ` `// repeating element found ` ` ` `if` `(m[arr[i]] == 1) ` ` ` `return` `arr[i]; ` ` ` `} ` ` ` `} ` ` ` ` ` `// return -1 if no repeating element exists ` ` ` `return` `-1; ` ` ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String []args) ` `{ ` ` ` `// read only array, not to be modified ` ` ` `int` `[] arr = { 1, 1, 2, 3, 5, 4 }; ` ` ` ` ` `// array of size 6(n + 1) having ` ` ` `// elements between 1 and 5 ` ` ` `int` `n = 5; ` ` ` ` ` `Console.WriteLine(` `"One of the numbers repeated in the array is: "` `+ ` ` ` `findRepeatingNumber(arr, n)); ` `} ` `} ` ` ` `// This code contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

**Output:**

One of the numbers repeated in the array is: 1

**Time Complexity**: O(N)

**Auxiliary Space**: sqrt(N)

This article is contributed by **Aanya Jindal**. 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.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Find any one of the multiple repeating elements in read only array | Set 2
- Find the two repeating elements in a given array
- Find sum of non-repeating (distinct) elements in an array
- Sum of all elements repeating 'k' times in an array
- Product of non-repeating (distinct) elements in an Array
- Find the first repeating element in an array of integers
- Find the only repeating element in a sorted array of size n
- Count of elements in an Array whose set bits are in a multiple of K
- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k
- Find K-th smallest element in an array for multiple queries
- Quickly find multiple left rotations of an array | Set 1
- Group multiple occurrence of array elements ordered by first occurrence
- Find original array from encrypted array (An array of sums of other elements)
- Find elements larger than half of the elements in an array
- Find Kth element in an array containing odd elements first and then even elements
- Find elements of array using XOR of consecutive elements
- Find all elements in array which have at-least two greater elements
- Maximum subarray sum in array formed by repeating the given array k times
- Find minimum value to assign all array elements so that array product becomes greater
- Find K such that changing all elements of the Array greater than K to K will make array sum N