# Remove duplicates from unsorted array using Set data structure

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.

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

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

Approach:

• Take a Set
• Insert all array element in the Set. Set does not allow duplicates and sets like LinkedHashSet maintains the order of insertion so it will remove duplicates and elements will be printed in the same order in which it is inserted.
• Convert the formed set into array.
• Print elements of Set.

Below is the implementation of the above approach:

## C++

 `// CPP program to remove duplicates ` `// from unsorted array ` `#include ` `using` `namespace` `std; ` ` `  `// Function to remove duplicate from array ` `void` `removeDuplicates(``int` `arr[], ``int` `n) ` `{ ` `    ``unordered_set<``int``> s; ` `     `  `    ``// adding elements to LinkedHashSet ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``s.insert(arr[i]); ` ` `  `    ``// Print the elements of LinkedHashSet ` `    ``cout << ``"[ "``; ` `       ``for``(``auto` `x:s) ` `    ``cout << x << ``" "``; ` `    ``cout << ``"]"``; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 1, 2, 5, 1, 7, 2, 4, 2 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]); ` `    ``removeDuplicates(arr, n); ` `} ` ` `  `// This code is contributed  ` `// by Surendra_Gangwar `

## Java

 `// Java program to remove duplicates ` `// from unsorted array ` ` `  `import` `java.util.*; ` ` `  `class` `GFG { ` ` `  `    ``// Function to remove duplicate from array ` `    ``public` `static` `void` `removeDuplicates(``int``[] arr) ` `    ``{ ` `        ``LinkedHashSet set ` `            ``= ``new` `LinkedHashSet(); ` ` `  `        ``// adding elements to LinkedHashSet ` `        ``for` `(``int` `i = ``0``; i < arr.length; i++) ` `            ``set.add(arr[i]); ` ` `  `        ``// Print the elements of LinkedHashSet ` `        ``System.out.print(set); ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `arr[] = { ``1``, ``2``, ``5``, ``1``, ``7``, ``2``, ``4``, ``2` `}; ` `        ``removeDuplicates(arr); ` `    ``} ` `} `

## Python3

 `# Python3 program to remove duplcates ` `def` `removeDulipcates(arr): ` `    ``s ``=` `dict``() ` `    ``d ``=` `[] ` ` `  `    ``for` `i ``in` `arr: ` `        ``if` `i ``not` `in` `s.keys(): ` `             `  `            ``# Adding elements to the dictionary ` `            ``d.append(i) ` `            ``s[i] ``=` `1` `     `  `    ``# Printing the dictionary ` `    ``print``(d) ` ` `  `# Driver Code ` `arr ``=` `[``1``, ``2``, ``5``, ``1``, ``7``, ``2``, ``4``, ``2``] ` `removeDulipcates(arr)      ` ` `  `# This code is contributed ` `# by Mohit Kumar `

## C#

 `// C# program to remove duplicates ` `// from unsorted array ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GFG  ` `{ ` ` `  `    ``// Function to remove duplicate from array ` `    ``public` `static` `void` `removeDuplicates(``int``[] arr) ` `    ``{ ` `        ``HashSet<``int``> ``set` `= ``new` `HashSet<``int``>(); ` ` `  `        ``// adding elements to LinkedHashSet ` `        ``for` `(``int` `i = 0; i < arr.Length; i++) ` `            ``set``.Add(arr[i]); ` ` `  `        ``// Print the elements of HashSet ` `        ``foreach``(``int` `item ``in` `set``) ` `            ``Console.Write(item + ``", "``); ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main(String[] args) ` `    ``{ ` `        ``int` `[]arr = { 1, 2, 5, 1, 7, 2, 4, 2 }; ` `        ``removeDuplicates(arr); ` `    ``} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

Output:

```[ 4 7 5 1 2 ]
```

Time Complexity: O(N)

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

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 :

Be the First to upvote.

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