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
- Count distinct elements in an array
- Distinct adjacent elements in an array
- Find sum of non-repeating (distinct) elements in an array
- Product of non-repeating (distinct) elements in an Array
- Distinct adjacent elements in a binary array
- Third largest element in an array of distinct elements
- Print All Distinct Elements of a given integer array
- Print sorted distinct elements of array
- Find minimum changes required in an array for it to contain k distinct elements
- Making elements distinct in a sorted array by minimum increments
- Count subarrays having total distinct elements same as original array
- Construct a distinct elements array with given size, sum and element upper bound
- Check if there exist two elements in an array whose sum is equal to the sum of rest of the array
- Queries for number of distinct elements from a given index till last index in an array
- Check if an array contains all elements of a given range
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.