Compruebe si todos los elementos de la array son distintos

Dada una array, compruebe si todos los elementos de una array son distintos o no.
Ejemplos: 
 

Input : 1, 3, 2, 4
Output : Yes

Input : "Geeks", "for", "Geeks"
Output : No

Input : "All", "Not", "Equal"
Output : Yes

Una solución simple es usar dos bucles anidados. Para cada elemento, compruebe si se repite o no. Si algún elemento se repite, devuelve falso. Si ningún elemento se repite, devuelve falso.
Una solución eficiente es Hashing. Ponemos todos los elementos de la array en un HashSet . Si el tamaño de HashSet sigue siendo el mismo que el tamaño de la array, devolvemos verdadero.
 

C++

// C++ program to check if all array
// elements are distinct
#include <bits/stdc++.h>
using namespace std;
 
bool areDistinct(vector<int> arr)
{
    int n = arr.size();
 
    // Put all array elements in a map
    unordered_set<int> s;
    for (int i = 0; i < n; i++) {
        s.insert(arr[i]);
    }
 
    // If all elements are distinct, size of
    // set should be same array.
    return (s.size() == arr.size());
}
 
// Driver code
int main()
{
    std::vector<int> arr = { 1, 2, 3, 2 };
 
    if (areDistinct(arr)) {
        cout << "All Elements are Distinct";
    }
    else {
        cout << "Not all Elements are Distinct";
    }
 
    return 0;
}

Java

// Java program to check if all array elements are
// distinct or not.
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
 
public class DistinctElements {
    public static boolean areDistinct(Integer arr[])
    {
        // Put all array elements in a HashSet
        Set<Integer> s =
           new HashSet<Integer>(Arrays.asList(arr));
 
        // If all elements are distinct, size of
        // HashSet should be same array.
        return (s.size() == arr.length);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        Integer[] arr = { 1, 2, 3, 2 };
        if (areDistinct(arr))
            System.out.println("All Elements are Distinct");
        else
            System.out.println("Not all Elements are Distinct");
    }
}

Python3

# Python3 program to check if all array
# elements are distinct
def areDistinct(arr) :
 
    n = len(arr)
 
    # Put all array elements in a map
    s = set()
    for i in range(0, n):
        s.add(arr[i])
     
    # If all elements are distinct,
    # size of set should be same array.
    return (len(s) == len(arr))
 
# Driver code
arr = [ 1, 2, 3, 2 ]
 
if (areDistinct(arr)):
    print("All Elements are Distinct")
 
else :
    print("Not all Elements are Distinct")
 
# This code is contributed by ihritik

C#

// C# program to check if all array elements are
// distinct or not.
using System;
using System.Collections.Generic;
 
public class DistinctElements
{
    public static bool areDistinct(int []arr)
    {
        // Put all array elements in a HashSet
        HashSet<int> s = new HashSet<int>(arr);
 
        // If all elements are distinct, size of
        // HashSet should be same array.
        return (s.Count == arr.Length);
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int[] arr = { 1, 2, 3, 2 };
        if (areDistinct(arr))
            Console.WriteLine("All Elements are Distinct");
        else
            Console.WriteLine("Not all Elements are Distinct");
    }
}
 
// This code has been contributed by 29AjayKumar

Javascript

<script>
// Javascript program to check if all array
// elements are distinct
function areDistinct(arr)
{
    let n = arr.length;
 
    // Put all array elements in a map
    let s = new Set();
    for (let i = 0; i < n; i++) {
        s.add(arr[i]);
    }
 
    // If all elements are distinct, size of
    // set should be same array.
    return (s.size == arr.length);
}
 
// Driver code
    let arr = [ 1, 2, 3, 2 ];
 
    if (areDistinct(arr)) {
        document.write("All Elements are Distinct");
    }
    else {
        document.write("Not all Elements are Distinct");
    }
 
// This code is contributed
// by _saurabh_jaiswal
</script>
Producción: 

Not all Elements are Distinct

 

Publicación traducida automáticamente

Artículo escrito por bilal-hungund 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 *