# Replace repeating elements with greater that greatest values

Given an integer array, if any integer is repeating then replace it with a number greater than that number which has not been inserted yet in the array.

Examples:

```Input : arr = {1, 3, 4, 5, 3}
Output : 1 3 4 5 6
Here 3 is repeating so it is replaced with 6

Input : arr = {1, 3, 4, 4, 5, 3}
Output : 1 3 4 6 5 7
```

We need to replace repeating numbers with a number that does not appear in the array and is one greater than the largest present in the array.

Calculate the maximum element in the array and replace the repeated elements with the maxx+1 and update the maxx element as visited.In implementation basic concept of hashing is used.

## C++

 `// CPP program to replace repeating elements ` `// with greater than the greatest. ` `#include ` `using` `namespace` `std; ` ` `  `void` `replaceElements(``int` `arr[], ``int` `n) ` `{ ` `    ``// Maximum element in an array ` `    ``int` `maxx = *max_element(arr, arr+n); ` ` `  `    ``unordered_set<``int``> s; ` ` `  `    ``for` `(``int` `i = 0; i < n; i++) { ` `          `  `        ``// check whether the element is  ` `        ``// repeated or not  ` `        ``if` `(s.find(arr[i]) == s.end())  ` `            ``s.insert(arr[i]); ` ` `  `        ``else` `{ ` ` `  `            ``// update the repeated element with the ` `            ``// maxx element ` `            ``arr[i] = maxx + 1; ` ` `  `            ``maxx++; ``// update the max ` ` `  `            ``// mark the maximum element as visited ` `            ``s.insert(maxx); ` `        ``} ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 1, 3, 4, 5, 3 }; ` `    ``int` `n = ``sizeof``(arr)/``sizeof``(arr); ` `    ``replaceElements(arr, n); ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``cout << arr[i] << ``" "``; ` `    ``return` `0; ` `} `

## Java

 `// Java  program to replace repeating elements ` `// with greater than the greatest. ` `import` `java.util.*; ` `class` `Solution ` `{ ` `    ``//returns the maximum element ` `    ``static` `int` `max_element(``int` `arr[]) ` `    ``{ ` `        ``int` `max=arr[``0``]; ` `        ``for``(``int` `i=``1``;i s=``new` `Vector(); ` `  `  `    ``for` `(``int` `i = ``0``; i < n; i++) { ` `           `  `        ``// check whether the element is  ` `        ``// repeated or not  ` `        ``if` `(!s.contains(arr[i]))  ` `            ``s.add(arr[i]); ` `  `  `        ``else` `{ ` `  `  `            ``// update the repeated element with the ` `            ``// maxx element ` `            ``arr[i] = maxx + ``1``; ` `  `  `            ``maxx++; ``// update the max ` `  `  `            ``// mark the maximum element as visited ` `            ``s.add(maxx); ` `        ``} ` `    ``} ` `} ` ` `  `// Driver code  ` `public` `static` `void` `main(String args[]) ` `{ ` `    ``int` `arr[] = { ``1``, ``3``, ``4``, ``5``, ``3` `}; ` `    ``int` `n = arr.length; ` `    ``replaceElements(arr, n); ` `    ``for` `(``int` `i = ``0``; i < n; i++) ` `        ``System.out.print( arr[i] + ``" "``); ` `     `  `} ` `} ` `//contributed by Arnab Kundu `

## Python3

 `# Python 3 program to replace repeating  ` `# elements with greater than the greatest. ` ` `  `def` `replaceElements( arr, n): ` ` `  `    ``# Maximum element in an array ` `    ``maxx ``=` `max``(arr) ` ` `  `    ``s ``=` `[] ` ` `  `    ``for` `i ``in` `range` `(n) : ` `         `  `        ``# check whether the element is  ` `        ``# repeated or not  ` `        ``if` `arr[i] ``not` `in` `s: ` `            ``s.append(arr[i]) ` ` `  `        ``else``: ` ` `  `            ``# update the repeated element  ` `            ``# with the maxx element ` `            ``arr[i] ``=` `maxx ``+` `1` ` `  `            ``maxx ``+``=` `1` `# update the max ` ` `  `            ``# mark the maximum element  ` `            ``# as visited ` `            ``s.append(maxx) ` ` `  `# Driver code ` `if` `__name__ ``=``=``"__main__"``: ` ` `  `    ``arr ``=` `[ ``1``, ``3``, ``4``, ``5``, ``3` `] ` `    ``n ``=` `len``(arr) ` `    ``replaceElements(arr, n) ` `    ``for` `i ``in` `range``( n): ` `        ``print` `(arr[i], end ``=` `" "``) ` ` `  `# This code is contributed by ita_c `

## C#

 `// C# program to replace repeating elements  ` `// with greater than the greatest.  ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GFG  ` `{  ` `    ``//returns the maximum element  ` `    ``static` `int` `max_element(``int` `[]arr)  ` `    ``{  ` `        ``int` `max = arr;  ` `        ``for``(``int` `i = 1; i < arr.Length; i++)  ` `        ``{  ` `            ``if``(max < arr[i])  ` `            ``max = arr[i];  ` `        ``}  ` `        ``return` `max;  ` `    ``}  ` `     `  `static` `void` `replaceElements(``int` `[]arr, ``int` `n)  ` `{  ` `    ``// Maximum element in an array  ` `    ``int` `maxx = max_element(arr);  ` ` `  `    ``List<``int``> s = ``new` `List<``int``>();  ` ` `  `    ``for` `(``int` `i = 0; i < n; i++)  ` `    ``{  ` `             `  `        ``// check whether the element is  ` `        ``// repeated or not  ` `        ``if` `(!s.Contains(arr[i]))  ` `            ``s.Add(arr[i]);  ` ` `  `        ``else`  `        ``{  ` ` `  `            ``// update the repeated element with the  ` `            ``// maxx element  ` `            ``arr[i] = maxx + 1;  ` ` `  `            ``maxx++; ``// update the max  ` ` `  `            ``// mark the maximum element as visited  ` `            ``s.Add(maxx);  ` `        ``}  ` `    ``}  ` `}  ` ` `  `// Driver code  ` `public` `static` `void` `Main()  ` `{  ` `    ``int` `[]arr = { 1, 3, 4, 5, 3 };  ` `    ``int` `n = arr.Length;  ` `    ``replaceElements(arr, n);  ` `    ``for` `(``int` `i = 0; i < n; i++)  ` `        ``Console.Write( arr[i] + ``" "``);  ` `}  ` `}  ` ` `  `// This code is contributed by PrinciRaj1992 `

Output:

```1 3 4 5 6
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

8

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