📌  相关文章
📜  使用 Map 数据结构从未排序的数组中删除重复项

📅  最后修改于: 2021-10-27 06:59:00             🧑  作者: Mango

给定一个未排序的整数数组,在从中删除重复元素后打印该数组。我们需要根据它们的第一次出现来打印不同的数组元素。
例子:

Input : arr[] = { 1, 2, 5, 1, 7, 2, 4, 2}
Output : 1 2 5 7 4
Explanation : {1, 2} appear more than one time.

方法 :

  • 取一个哈希图,它将存储之前出现过的所有元素。
  • 遍历数组。
  • 检查该元素是否存在于哈希映射中。
  • 如果是,继续遍历数组。
  • 否则打印元素。
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 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 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 mp = new Dictionary();
  
        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


输出:

1 2 5 7 4

时间复杂度 – O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程