Check if all array elements are distinct
Last Updated :
11 Jul, 2025
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 True. 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++
// 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>
OutputNot all Elements are Distinct
Time complexity: O(n) where n is the size of the given array
Auxiliary space: O(n)
Similar Reads
Java Program to Remove Duplicate Elements From the Array Given an array, the task is to remove the duplicate elements from an array. The simplest method to remove duplicates from an array is using a Set, which automatically eliminates duplicates. This method can be used even if the array is not sorted.Example:Java// Java Program to Remove Duplicate // Ele
6 min read
How to Efficiently Remove Duplicates from an Array without using Set? Arrays are a fundamental data structure in Java that stores data of the same type in contiguous memory locations. Removing duplicate elements from an array is a common operation that can be easily accomplished using sets. However, in this article, we will learn how to remove duplicates from an array
2 min read
How to Get Unique Values from ArrayList using Java 8? ArrayList in Java do not prevent the list from having duplicate values. But there are ways if you want to get unique values from the ArrayList and each way is explained with an example. Method 1(Using Stream API's distinct() Method): For Java 8, You can use Java 8 Stream API. To get distinct values,
3 min read
Distinct adjacent elements in a binary array Given a binary array arr[] of 1's and 0's of length N. The task is to find the number of elements that are different with respect to their neighbors. Note: At least one of the neighbors should be distinct. Examples: Input : N = 4 , arr=[1, 0, 1, 1] Output : 3 arr[0]=1 is distinct since it's neighbor
5 min read
Distinct adjacent elements in an array Given an array, find whether it is possible to obtain an array having distinct neighbouring elements by swapping two neighbouring array elements. Examples: Input : 1 1 2 Output : YES swap 1 (second last element) and 2 (last element), to obtain 1 2 1, which has distinct neighbouring elements . Input
7 min read
Check if an array contains only one distinct element Given an array arr[] of size N, the task is to check if the array contains only one distinct element or not. If it contains only one distinct element then print âYesâ, otherwise print âNoâ. Examples: Input: arr[] = {3, 3, 4, 3, 3} Output: No Explanation: There are 2 distinct elements present in the
8 min read