Check if all array elements are distinct

Given an array, check whether all elements in an array are distinct or not.

Examples:

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

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

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

One simple solution is to use two nested loops. For every element, check if it repeats or not. If any element repeats, return false. If no element repeats, return false.

An efficient solution is to Hashing. We put all array elements in a HashSet. If size of HashSet remains same as array size, then we return true.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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");
    }
}

chevron_right


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#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

Not all Elements are Distinct


My Personal Notes arrow_drop_up

सर्वशक्तिशाली इकलौता

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.