Remove duplicates from unsorted array using Set data structure
Last Updated :
11 Aug, 2022
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++
// CPP program to remove duplicates
// from unsorted array
#include <bits/stdc++.h>
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]);
// Function call
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<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
# Python3 program to remove duplicates
def removeDulipcates(arr):
# convert the arr into set and then into list
return list(set(arr))
# Driver Code
arr = [1, 2, 5, 1, 7, 2, 4, 2]
# Function call
print(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 };
// Function call
removeDuplicates(arr);
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// Javascript program to remove duplicates
// from unsorted array
// Function to remove duplicate from array
function 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 code
let arr = [ 1, 2, 5, 1, 7, 2, 4, 2 ];
// Function call
removeDuplicates(arr);
// This code is contributed by patel2127
</script>
Time Complexity: O(N)
Auxiliary Space: O(N) as using unordered_set
Similar Reads
Remove duplicates from unsorted array using Map 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. App
4 min read
Remove duplicates from Sorted Array Given a sorted array arr[] of size n, the goal is to rearrange the array so that all distinct elements appear at the beginning in sorted order. Additionally, return the length of this distinct sorted subarray.Note: The elements after the distinct ones can be in any order and hold any value, as they
7 min read
Remove Duplicates from an Unsorted Linked List Given an unsorted linked list containing n nodes, the task is to remove duplicate nodes while preserving the original order.Examples:Input: 12 -> 11 -> 12 -> 21 -> 41 -> 43 -> 21 Output: 12 -> 11 -> 21 -> 41 -> 43 Explanation: The second occurrence of 12 (the one after
14 min read
Duplicates Removal in Array using BST Given an array arr[] of integers, the task is to remove duplicates from the given array. Examples: Input: arr[] = {1, 2, 3, 2, 5, 4, 4} Output: arr[] = {1, 2, 3, 4, 5} Input: arr[] = {127, 234, 127, 654, 355, 789, 355, 355, 999, 654} Output: arr[] = {127, 234, 355, 654, 789, 999} The duplicates in t
7 min read
Remove duplicates from a sorted linked list Given a linked list sorted in non-decreasing order. Return the list by deleting the duplicate nodes from the list. The returned list should also be in non-decreasing order.Example:Input : Linked List = 11->11->11->21->43->43->60Output : 11->21->43->60Explanation:Remove dup
15+ min read