# Find duplicates in a given array when elements are not limited to a range

Given an array of n integers. The task is to print the duplicates in the given array. If there are no duplicates then print -1.

Examples:

```Input : {2, 10, 100, 2, 10, 11}
Output : 2 10

Input : {5, 40, 1, 40, 100000, 1, 5, 1}
Output : 5 40 1
```

Note:The duplicate elements can be printed in any order.

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

Simple Approach: By using two loops. It has a time complexity of O(n2).

Efficient Approach: Use unordered_map for hashing. Count frequency of occurrence of each element and the elements with frequency more than 1 is printed. unordered_map is used as range of integers is not known. For Python, Use Dictionary to store number as key and it’s frequency as value. Dictionary can be used as range of integers is not known.

## C++

 `// C++ program to find ` `// duplicates in the given array ` `#include ` `using` `namespace` `std; ` ` `  `// function to find and print duplicates ` `void` `printDuplicates(``int` `arr[], ``int` `n) ` `{ ` `    ``// unordered_map to store frequencies ` `    ``unordered_map<``int``, ``int``> freq; ` `    ``for` `(``int` `i=0; i:: iterator itr; ` `    ``for` `(itr=freq.begin(); itr!=freq.end(); itr++) ` `    ``{ ` `        ``// if frequency is more than 1 ` `        ``// print the element ` `        ``if` `(itr->second > 1) ` `        ``{ ` `            ``cout << itr->first << ``" "``; ` `            ``dup = ``true``; ` `        ``} ` `    ``} ` ` `  `    ``// no duplicates present ` `    ``if` `(dup == ``false``) ` `        ``cout << ``"-1"``; ` `} ` ` `  `// Driver program to test above ` `int` `main() ` `{ ` `    ``int` `arr[] = {12, 11, 40, 12, 5, 6, 5, 12, 11}; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``printDuplicates(arr, n); ` `    ``return` `0; ` `} `

## Java

 `// Java program to find ` `// duplicates in the given array ` `import` `java.util.HashMap; ` `import` `java.util.Map; ` `import` `java.util.Map.Entry; ` ` `  `public` `class` `FindDuplicatedInArray ` `{ ` `    ``// Driver program ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `arr[] = {``12``, ``11``, ``40``, ``12``, ``5``, ``6``, ``5``, ``12``, ``11``}; ` `        ``int` `n = arr.length; ` `        ``printDuplicates(arr, n); ` `    ``} ` `    ``// function to find and print duplicates ` `    ``private` `static` `void` `printDuplicates(``int``[] arr, ``int` `n)  ` `    ``{ ` `        ``Map map = ``new` `HashMap<>(); ` `        ``int` `count = ``0``; ` `        ``boolean` `dup = ``false``; ` `        ``for``(``int` `i = ``0``; i < n; i++){ ` `            ``if``(map.containsKey(arr[i])){ ` `                ``count = map.get(arr[i]); ` `                ``map.put(arr[i], count + ``1``); ` `            ``} ` `            ``else``{ ` `                ``map.put(arr[i], ``1``); ` `            ``} ` `        ``} ` `         `  `        ``for``(Entry entry : map.entrySet()) ` `        ``{ ` `            ``// if frequency is more than 1 ` `            ``// print the element ` `            ``if``(entry.getValue() > ``1``){ ` `                ``System.out.print(entry.getKey()+ ``" "``); ` `                ``dup = ``true``; ` `            ``} ` `        ``} ` `        ``// no duplicates present ` `        ``if``(!dup){ ` `            ``System.out.println(``"-1"``); ` `        ``} ` `    ``} ` `} `

## Python3

 `# Python 3 code to find duplicates  ` `# using dictionary approach. ` `def` `printDuplicates(arr): ` `    ``dict` `=` `{} ` ` `  `    ``for` `ele ``in` `arr: ` `        ``try``: ` `            ``dict``[ele] ``+``=` `1` `        ``except``: ` `            ``dict``[ele] ``=` `1` `             `  `    ``for` `item ``in` `dict``: ` `         `  `         ``# if frequency is more than 1 ` `         ``# print the element ` `        ``if``(``dict``[item] > ``1``): ` `            ``print``(item, end``=``" "``) ` ` `  `    ``print``(``"\n"``) ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `"__main__"``: ` `    ``list` `=` `[``12``, ``11``, ``40``, ``12``,  ` `            ``5``, ``6``, ``5``, ``12``, ``11``] ` `    ``printDuplicates(``list``) ` ` `  `# This code is contributed ` `# by Sushil Bhile `

## C#

 `// C# program to find  ` `// duplicates in the given array  ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GFG ` `{ ` `    ``// function to find and print duplicates ` `    ``static` `void` `printDuplicates(``int``[] arr, ``int` `n) ` `    ``{ ` `        ``Dictionary<``int``,  ` `                   ``int``> map = ``new` `Dictionary<``int``,  ` `                                             ``int``>(); ` `        ``int` `count = 0; ` `        ``bool` `dup = ``false``; ` `        ``for` `(``int` `i = 0; i < n; i++) ` `        ``{ ` `            ``if` `(map.ContainsKey(arr[i])) ` `            ``{ ` `                ``count = map[arr[i]]; ` `                ``map[arr[i]]++; ` `            ``} ` `            ``else` `                ``map.Add(arr[i], 1); ` `        ``} ` ` `  `        ``foreach` `(KeyValuePair<``int``,  ` `                              ``int``> entry ``in` `map) ` `        ``{ ` `            ``// if frequency is more than 1  ` `            ``// print the element ` `            ``if` `(entry.Value > 1) ` `                ``Console.Write(entry.Key + ``" "``); ` `            ``dup = ``true``; ` `        ``} ` ` `  `        ``// no duplicates present ` `        ``if` `(!dup) ` `            ``Console.WriteLine(``"-1"``); ` `    ``} ` ` `  `    ``// Driver Code  ` `    ``public` `static` `void` `Main(String[] args) ` `    ``{ ` `        ``int``[] arr = { 12, 11, 40, 12,  ` `                     ``5, 6, 5, 12, 11 }; ` `        ``int` `n = arr.Length; ` `        ``printDuplicates(arr, n); ` `    ``} ` `} ` ` `  `// This code is contributed by ` `// sanjeev2552 `

Output:

```12 11 5
```

Time Complexity: O(n)

This article is contributed by Ayush Jauhari. 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.

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.

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

4

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