Remove duplicates from unsorted array using Map data structure Last Updated : 07 Sep, 2022 Summarize Comments Improve Suggest changes Share Like Article Like 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> Output1 2 5 7 4 Complexity Analysis: Time Complexity: O(N)Auxiliary Space: O(N) Comment More infoAdvertise with us Next Article Remove duplicates from unsorted array using Map data structure I imdhruvgupta Follow Improve Article Tags : Hash Technical Scripter DSA Arrays Technical Scripter 2018 cpp-unordered_map +2 More Practice Tags : ArraysHash 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: 3 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 Introduction to Map â Data Structure and Algorithm Tutorials Maps is also known as dictionaries or associative arrays, are fundamental data structures that allow you to efficiently store and retrieve data based on unique keys. This tutorial will cover the basics of maps, including their main ideas, how they are used in different programming languages, and how 15+ min read Remove elements to make array sorted Given an array of integers, the task is to remove elements from the array to make the array sorted. That is, remove the elements which do not follow an increasing order. Examples: Input: arr[] = {1, 2, 4, 3, 5, 7, 8, 6, 9, 10} Output: 1 2 4 5 7 8 9 10 Input: arr[] = {10, 12, 9, 5, 2, 13, 14} Output: 8 min read Merge two Maps of Array into one sorted Map of Array Given two maps map1 and map2 having a string as the key and arrays of integers as values, the task is to merge them in one map such that if a key is common in both the maps, the respective arrays should be merged. Examples: Input: map1 = { ("key1", {0, 1}), ("key2", {0, 1}) }, map2 = { ("key2", {1, 12 min read Like