In: Computer Science
Consider the element uniqueness problem: check
whether all the elements in a given array of n elements are
distinct
answer in pseudo code places
The pseudocode for the element uniqueness problem to check whether all the elements in a given array of 'n' elements are distinct is given below:
1. Determine the size of the array of n elements and let it be N.
2. Put every element present in the array into the hash-set.
3. Determine the size of hash set and let it be S.
4. If N = S, return True.
5. Else, if N is not equal to S, return False.
*******************************
Explanation of the pseudocode: We will use and create a hash-set for every element of the array. That is, we will iterate and traverse through the entire array and then put every element of the array into the hash set.
Since a hash-set is a set that does not allows duplicate values in it and only has distinct elements in it, we will check the size of the hash-set after putting all the elements of the array into it.
Now, if the size of array is equal to the size of the hash set, then there are no duplicate elements in the array and hence all elements are distinct. We will then return True in this case. Otherwise, if the size of array is not equal to size of hash set that is if the size of hash set is smaller than the size of the array, then the array has duplicate elements and hence we will return False.
Thanks!