Open In App

Remove duplicates from unsorted array using Map data structure

Last Updated : 07 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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, which will store all the elements which have 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.

Implementation:

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 not
        // 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;
}


Java




// Java program to remove
// the duplicates from the array.
import java.util.HashMap;
 
class GFG
{
    static void removeDups(int[] arr, int n)
    {
 
        // Hash map which will store the
        // elements which has appeared previously.
        HashMap<Integer,
                Boolean> mp = new HashMap<>();
 
        for (int i = 0; i < n; ++i)
        {
 
            // Print the element if it is not
            // there in the hash map
            if (mp.get(arr[i]) == null)
                System.out.print(arr[i] + " ");
 
            // Insert the element in the hash map
            mp.put(arr[i], true);
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int[] arr = { 1, 2, 5, 1, 7, 2, 4, 2 };
        int n = arr.length;
        removeDups(arr, n);
    }
}
 
// This code is contributed by
// sanjeev2552


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


C#




// C# program to remove
// the duplicates from the array.
using System;
using System.Collections.Generic;
 
class GFG
{
    static void removeDups(int[] arr, int n)
    {
  
        // Hash map which will store the
        // elements which has appeared previously.
        Dictionary<int,
                Boolean> mp = new Dictionary<int, Boolean>();
  
        for (int i = 0; i < n; ++i)
        {
  
            // Print the element if it is not
            // there in the hash map
            if (!mp.ContainsKey(arr[i]))
                Console.Write(arr[i] + " ");
  
            // Insert the element in the hash map
            mp[arr[i]] = true;
        }
    }
  
    // Driver Code
    public static void Main(String[] args)
    {
        int[] arr = { 1, 2, 5, 1, 7, 2, 4, 2 };
        int n = arr.Length;
        removeDups(arr, n);
    }
}
 
// This code is contributed by Rajput-Ji


Javascript




<script>
 
// JavaScript program to remove
// the duplicates from the array.
 
function removeDups(arr,n)
{
    // Hash map which will store the
        // elements which has appeared previously.
        let mp = new Map();
   
        for (let i = 0; i < n; ++i)
        {
   
            // Print the element if it is not
            // there in the hash map
            if (mp.get(arr[i]) == null)
                document.write(arr[i] + " ");
   
            // Insert the element in the hash map
            mp.set(arr[i], true);
        }
}
 
// Driver Code
let arr=[1, 2, 5, 1, 7, 2, 4, 2 ];
let n = arr.length;
removeDups(arr, n);
 
 
// This code is contributed by unknown2108
 
</script>


Output

1 2 5 7 4 

Complexity Analysis:

  • Time Complexity: O(N)
  • Auxiliary Space: O(N)


Similar Reads

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 Approach:
3 min read
Remove duplicates from an unsorted doubly linked list
Given an unsorted doubly linked list containing n nodes. The problem is to remove duplicate nodes from the given list. Examples: Method 1 (Naive Approach): This is the simplest way where two loops are used. The outer loop is used to pick the elements one by one and the inner loop compares the picked element with the rest of the elements. Implementa
15+ min read
Remove Duplicates from an Unsorted Linked List
Given an unsorted Linked List, the task is to remove duplicates from the list. Examples: Input: linked_list = 12 -&gt; 11 -&gt; 12 -&gt; 21 -&gt; 41 -&gt; 43 -&gt; 21 Output: 12 -&gt; 11 -&gt; 21 -&gt; 41 -&gt; 43 Explanation: Second occurrence of 12 and 21 are removed. Input: linked_list = 12 -&gt; 11 -&gt; 12 -&gt; 21 -&gt; 41 -&gt; 43 -&gt; 21 O
14 min read
C++ Program For Removing Duplicates From An Unsorted Linked List
Write a removeDuplicates() function that takes a list and deletes any duplicate nodes from the list. The list is not sorted. For example if the linked list is 12-&gt;11-&gt;12-&gt;21-&gt;41-&gt;43-&gt;21 then removeDuplicates() should convert the list to 12-&gt;11-&gt;21-&gt;41-&gt;43. Recommended: Please solve it on "PRACTICE" first, before moving
4 min read
Java Program For Removing Duplicates From An Unsorted Linked List
Given an unsorted Linked List, the task is to remove duplicates from the list. Examples: Input: linked_list = 12 -&gt; 11 -&gt; 12 -&gt; 21 -&gt; 41 -&gt; 43 -&gt; 21 Output: 12 -&gt; 11 -&gt; 21 -&gt; 41 -&gt; 43 Explanation: Second occurrence of 12 and 21 are removed. Input: linked_list = 12 -&gt; 11 -&gt; 12 -&gt; 21 -&gt; 41 -&gt; 43 -&gt; 21 O
6 min read
Python Program For Removing Duplicates From An Unsorted Linked List
Write a removeDuplicates() function that takes a list and deletes any duplicate nodes from the list. The list is not sorted. For example if the linked list is 12-&gt;11-&gt;12-&gt;21-&gt;41-&gt;43-&gt;21 then removeDuplicates() should convert the list to 12-&gt;11-&gt;21-&gt;41-&gt;43. Recommended: Please solve it on "PRACTICE" first, before moving
4 min read
C# Program For Removing Duplicates From An Unsorted Linked List
Write a removeDuplicates() function that takes a list and deletes any duplicate nodes from the list. The list is not sorted. For example if the linked list is 12-&gt;11-&gt;12-&gt;21-&gt;41-&gt;43-&gt;21 then removeDuplicates() should convert the list to 12-&gt;11-&gt;21-&gt;41-&gt;43. Recommended: Please solve it on "PRACTICE" first, before moving
4 min read
Javascript Program For Removing Duplicates From An Unsorted Linked List
Given an unsorted Linked List, the task is to remove duplicates from the list. Examples: Input: linked_list = 12 -&gt; 11 -&gt; 12 -&gt; 21 -&gt; 41 -&gt; 43 -&gt; 21 Output: 12 -&gt; 11 -&gt; 21 -&gt; 41 -&gt; 43 Explanation: Second occurrence of 12 and 21 are removed. Input: linked_list = 12 -&gt; 11 -&gt; 12 -&gt; 21 -&gt; 41 -&gt; 43 -&gt; 21 O
5 min read
Check if two unsorted arrays (with duplicates allowed) have same elements
Given two unsorted arrays, check whether both arrays have the same set of elements or not. Examples: Input : A = {2, 5, 6, 8, 10, 2, 2} B = {2, 5, 5, 6, 8, 5, 6} Output : No Input : A = {2, 5, 6, 8, 2, 10, 2} B = {2, 5, 6, 8, 2, 10, 2} Output : Yes Input : A = {2, 5, 8, 6, 10, 2, 2} B = {2, 5, 6, 8, 2, 10, 2} Output : Yes Method 1 (Simple):A simple
13 min read
Design a data structure that supports insert, delete, getRandom in O(1) with duplicates
Design a Data Structure that can support the following operations in O(1) Time Complexity. insert(x): Inserts x in the data structure. Returns True if x was not present and False if it was already present.remove(x): Removes x from the data structure, if present.getRandom(): Returns any value present in the stream randomly. The probability of each e
9 min read
three90RightbarBannerImg