# Array elements that appear more than once

Given an integer array, print all repeating elements (Elements that appear more than once) in the array. The output should contain elements according to their first occurrences.

Examples:

```Input: arr[] = {12, 10, 9, 45, 2, 10, 10, 45}
Output: 10 45

Input: arr[] = {1, 2, 3, 4, 2, 5}
Output: 2

Input: arr[] = {1, 1, 1, 1, 1}
Output: 1```

## Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

The idea is to use Hashing to solve this in O(n) time on average. We store elements and their counts in a hash table. After storing counts, we traverse input array again and print those elements whose counts are more than once. To make sure that every output element is printed only once, we set count as 0 after printing the element.

## C++

 `// C++ program to print all repeating elements ` `#include ` `using` `namespace` `std; ` ` `  `void` `printRepeating(``int` `arr[], ``int` `n) ` `{ ` `    ``// Store elements and their counts in ` `    ``// hash table ` `    ``unordered_map<``int``, ``int``> mp; ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``mp[arr[i]]++; ` ` `  `    ``// Since we want elements in same order, ` `    ``// we traverse array again and print ` `    ``// those elements that appear more than ` `    ``// once. ` `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``if` `(mp[arr[i]] > 1) { ` `            ``cout << arr[i] << ``" "``; ` ` `  `            ``// This is tricky, this is done ` `            ``// to make sure that the current ` `            ``// element is not printed again ` `            ``mp[arr[i]] = 0; ` `        ``} ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 12, 10, 9, 45, 2, 10, 10, 45 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]); ` `    ``printRepeating(arr, n); ` `    ``return` `0; ` `} `

## Java

 `// Java program to print all repeating elements ` ` `  `import` `java.util.*; ` `import` `java.util.Map.Entry; ` `import` `java.io.*; ` `import` `java.lang.*; ` ` `  `public` `class` `GFG { ` ` `  `    ``static` `void` `printRepeating(``int` `arr[], ``int` `n) ` `    ``{ ` ` `  `        ``// Store elements and their counts in ` `        ``// hash table ` `        ``Map map ` `            ``= ``new` `LinkedHashMap(); ` `        ``for` `(``int` `i = ``0``; i < n; i++) { ` `            ``try` `{ ` `                ``map.put(arr[i], map.get(arr[i]) + ``1``); ` `            ``} ` `            ``catch` `(Exception e) { ` `                ``map.put(arr[i], ``1``); ` `            ``} ` `        ``} ` ` `  `        ``// Since we want elements in the same order, ` `        ``// we traverse array again and print ` `        ``// those elements that appear more than once. ` ` `  `        ``for` `(Entry e : map.entrySet()) { ` `            ``if` `(e.getValue() > ``1``) { ` `                ``System.out.print(e.getKey() + ``" "``); ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ``throws` `IOException ` `    ``{ ` `        ``int` `arr[] = { ``12``, ``10``, ``9``, ``45``, ``2``, ``10``, ``10``, ``45` `}; ` `        ``int` `n = arr.length; ` `        ``printRepeating(arr, n); ` `    ``} ` `} ` ` `  `// This code is contributed by Wrick `

## Python3

 `# Python3 program to print ` `# all repeating elements ` `def` `printRepeating(arr, n): ` ` `  `    ``# Store elements and  ` `    ``# their counts in ` `    ``# hash table ` `    ``mp ``=` `[``0``] ``*` `100` `    ``for` `i ``in` `range``(``0``, n): ` `        ``mp[arr[i]] ``+``=` `1` ` `  `    ``# Since we want elements  ` `    ``# in same order, we  ` `    ``# traverse array again  ` `    ``# and print those elements  ` `    ``# that appear more than once. ` `    ``for` `i ``in` `range``(``0``, n): ` `        ``if` `(mp[arr[i]] > ``1``): ` `            ``print``(arr[i], end ``=` `" "``) ` `             `  `            ``# This is tricky, this  ` `            ``# is done to make sure  ` `            ``# that the current element  ` `            ``# is not printed again ` `            ``mp[arr[i]] ``=` `0` `     `  `# Driver code ` `arr ``=` `[``12``, ``10``, ``9``, ``45``,  ` `       ``2``, ``10``, ``10``, ``45``]  ` `n ``=` `len``(arr) ` `printRepeating(arr, n) ` ` `  `# This code is contributed  ` `# by Smita `

## C#

 `// C# program to print all repeating elements ` `using` `System; ` `using` `System.Collections.Generic;  ` ` `  `class` `GFG  ` `{ ` `static` `void` `printRepeating(``int` `[]arr, ``int` `n) ` `{ ` ` `  `    ``// Store elements and their counts in ` `    ``// hash table ` `    ``Dictionary<``int``,  ` `               ``int``> map = ``new` `Dictionary<``int``, ` `                                         ``int``>(); ` `    ``for` `(``int` `i = 0 ; i < n; i++) ` `    ``{ ` `        ``if``(map.ContainsKey(arr[i])) ` `        ``{ ` `            ``var` `val = map[arr[i]]; ` `            ``map.Remove(arr[i]); ` `            ``map.Add(arr[i], val + 1);  ` `        ``} ` `        ``else` `        ``{ ` `            ``map.Add(arr[i], 1); ` `        ``} ` `    ``} ` ` `  `    ``// Since we want elements in the same order, ` `    ``// we traverse array again and print ` `    ``// those elements that appear more than once. ` `    ``foreach``(KeyValuePair<``int``, ``int``> e ``in` `map) ` `    ``{ ` `        ``if` `(e.Value > 1)  ` `        ``{ ` `            ``Console.Write(e.Key + ``" "``); ` `        ``} ` `    ``} ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``int` `[]arr = { 12, 10, 9, 45, 2, 10, 10, 45 }; ` `    ``int` `n = arr.Length; ` `    ``printRepeating(arr, n); ` `} ` `} ` ` `  `// This code is contributed by PrinciRaj1992  `

Output:

```10 45
```

Time Complexity: O(n) under the assumption that hash insert and search functions work in O(1) time.

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 :

1

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