Finding Distinct Elements in an array using C++
Find all Distinct Elements in an unsorted array using C++
A number with one or more than one occurrence in an array considered only once is called a distinct element.We can find all such distinct elements in an unsorted array using a hash array of integers in which each element has an assigned index and if that element is repeated we change the flag value to 1 and print the distinct element.
Sample Test Case
Enter size of array : 5
Enter elements of array : 5 8 7 8 5
Distinct Elements : 5 8 7
- Declare size of array.
- Declare the given array.
- Initialize the hash array to 0.
- Traverse the whole array and increment the index value of hash array.
- If element is not present at given index increment the flag and print the Distinct Element.
This page is contributed by Rishav Raj