Given an array, check whether all elements in an array are distinct or not.
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.
Not all Elements are Distinct
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Length of array pair formed where one contains all distinct elements and other all same elements
- Count all distinct pairs of repeating elements from the array for every array element
- Check if Array elements can be maximized upto M by adding all elements from another array
- Minimum elements to change so that for an index i all elements on the left are -ve and all elements on the right are +ve
- Check if K distinct array elements form an odd sum
- Split an array containing N elements into K sets of distinct elements
- Print All Distinct Elements of a given integer array
- Minimize the cost to make all the adjacent elements distinct in an Array
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Count of Array elements greater than all elements on its left and next K elements on its right
- Form an array of distinct elements with each element as sum of an element from each array
- Count distinct elements after adding each element of First Array with Second Array
- Sum of distinct elements when elements are in range 1 to n
- Maximum distinct elements after removing k elements
- Check if all sub-numbers have distinct Digit product
- Check if N leaves only distinct remainders on division by all values up to K
- Find distinct elements common to all rows of a matrix
- Check if all the elements can be made of same parity by inverting adjacent elements
- Arrange N elements in circular fashion such that all elements are strictly less than sum of adjacent elements
- Check if an array contains only one distinct element
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.