Remove duplicates from unsorted array using Set data structure

03 Sep, 2021
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
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++
Java
Python3
C#
Javascript
// CPP program to remove duplicates// from unsorted array#include <bits/stdc++.h>using namespace std; // Function to remove duplicate from arrayvoid 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 codeint main(){    int arr[] = { 1, 2, 5, 1, 7, 2, 4, 2 };    int n = sizeof(arr) / sizeof(arr[0]);       // Function call    removeDuplicates(arr, n);} // This code is contributed// by Surendra_Gangwar
// 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<Integer> set            = new LinkedHashSet<Integer>();         // 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 };               // Function call        removeDuplicates(arr);    }}
# Python3 program to remove duplicatesdef removeDulipcates(arr):       # convert the arr into set and then into list    return list(set(arr))  # Driver Codearr = [1, 2, 5, 1, 7, 2, 4, 2] # Function callprint(removeDulipcates(arr)) # This code is contributed# by Mohit Kumar
// C# program to remove duplicates// from unsorted arrayusing 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 };               // Function call        removeDuplicates(arr);    }} // This code is contributed by 29AjayKumar
<script> // Javascript program to remove duplicates// from unsorted array // Function to remove duplicate from arrayfunction removeDuplicates(arr){    let set = new Set();         // Adding elements to LinkedHashSet    for(let i = 0; i < arr.length; i++)        set.add(arr[i]);         // Print the elements of LinkedHashSet    document.write("[" + Array.from(set).join(", ") + "]");}  // Driver codelet arr = [ 1, 2, 5, 1, 7, 2, 4, 2 ]; // Function callremoveDuplicates(arr); // This code is contributed by patel2127 </script>
[ 4 7 5 1 2 ]
Time Complexity: O(N)
mohit kumar 29
29AjayKumar
SURENDRA_GANGWAR
surangatco
santanupatra47
patel2127
simmytarika5
java-LinkedHashSet
Arrays
Arrays
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.