VOOZH about

URL: https://www.geeksforgeeks.org/dsa/remove-duplicates-from-unsorted-array-using-map-data-structure/

⇱ Remove duplicates from unsorted array using Map data structure - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Remove duplicates from unsorted array using Map data structure

Last Updated : 7 Sep, 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.

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:


Output
1 2 5 7 4 

Complexity Analysis:

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