Elimine los duplicados de una array sin clasificar utilizando la estructura de datos del mapa

Dada una array no ordenada de enteros, imprima la array después de eliminar los elementos duplicados. Necesitamos imprimir elementos de array distintos de acuerdo con su primera aparición.
Ejemplos: 
 

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

Acercarse : 
 

  • Tome un mapa hash, que almacenará todos los elementos que han aparecido antes.
  • Atraviesa la array.
  • Compruebe si el elemento está presente en el mapa hash.
  • En caso afirmativo, continúe recorriendo la array.
  • De lo contrario, imprime el elemento.

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>
Producción: 

1 2 5 7 4

 

Complejidad del tiempo – O(N)

Espacio Auxiliar – O(N).
 

Publicación traducida automáticamente

Artículo escrito por imdhruvgupta y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *