# Remove duplicates from unsorted array

Given an unsorted array of integers, Print the array after removing the duplicate elements from it. We need to print distinct array elements according to their first occurrence.

**Examples:**

Input : arr[] = { 1, 2, 5, 1, 7, 2, 4, 2} Output : 1 2 5 7 4 Explanation : {1, 2} appear more than one time.

**Approach :**

- Take a hash map, in which will store all the elements which has appeared before.
- Traverse the array.
- Check if the element is present in the hash map.
- If yes, continue traversing the array.
- Else Print the element.

## C++

`// C++ program to remove the duplicates from the array. ` `#include "iostream" ` `#include "unordered_map" ` `using` `namespace` `std; ` ` ` `void` `removeDups(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `// Hash map which will store the ` ` ` `// elements which has appeared previously. ` ` ` `unordered_map<` `int` `, ` `bool` `> mp; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; ++i) { ` ` ` ` ` `// Print the element if it is ` ` ` `// there in the hash map ` ` ` `if` `(mp.find(arr[i]) == mp.end()) { ` ` ` `cout << arr[i] << ` `" "` `; ` ` ` `} ` ` ` ` ` `// Insert the element in the hash map ` ` ` `mp[arr[i]] = ` `true` `; ` ` ` `} ` `} ` ` ` `int` `main(` `int` `argc, ` `char` `const` `* argv[]) ` `{ ` ` ` `int` `arr[] = { 1, 2, 5, 1, 7, 2, 4, 2 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `removeDups(arr, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to remove the ` `# duplicates from the array ` `def` `removeDups(arr, n): ` ` ` ` ` `# dict to store every element ` ` ` `# one time ` ` ` `mp ` `=` `{i : ` `0` `for` `i ` `in` `arr} ` ` ` ` ` `for` `i ` `in` `range` `(n): ` ` ` ` ` `if` `mp[arr[i]] ` `=` `=` `0` `: ` ` ` `print` `(arr[i], end ` `=` `" "` `) ` ` ` `mp[arr[i]] ` `=` `1` ` ` `# Driver code ` `arr ` `=` `[ ` `1` `, ` `2` `, ` `5` `, ` `1` `, ` `7` `, ` `2` `, ` `4` `, ` `2` `] ` ` ` `# len of array ` `n ` `=` `len` `(arr) ` ` ` `removeDups(arr,n) ` ` ` `# This code is contributed ` `# by Mohit Kumar ` |

*chevron_right*

*filter_none*

**Output:**

1 2 5 7 4

**Time Complexity –** O(N)

## Recommended Posts:

- Remove duplicates from sorted array
- Remove duplicates from an array of small primes
- Check if two unsorted arrays (with duplicates allowed) have same elements
- Recursively remove all adjacent duplicates
- Why is it faster to process sorted array than an unsorted array ?
- k-th missing element in an unsorted array
- Program for Mean and median of an unsorted array
- Find the two numbers with odd occurrences in an unsorted array
- Find k closest numbers in an unsorted array
- Find floor and ceil in an unsorted array
- K'th Smallest/Largest Element in Unsorted Array | Set 1
- Search, insert and delete in an unsorted array
- Print all pairs in an unsorted array with equal sum
- Front and Back Search in unsorted array
- Cumulative frequency of count of each element in an unsorted array

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.