Question

In: Computer Science

Consider the element uniqueness problem: check whether all the elements in a given array of n...

Consider the element uniqueness problem: check
whether all the elements in a given array of n elements are distinct

answer in pseudo code places

Solutions

Expert Solution

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!


Related Solutions

Problem 1: Given an array A[0 ... n-1], where each element of the array represents a...
Problem 1: Given an array A[0 ... n-1], where each element of the array represents a vote in the election. Assume that each vote is given as integers representing the ID of the chosen candidate. Write the code determining who wins the election. Problem 2: How do we find the number which appeared maximum number of times in an array? ( Use Java and an original code )
write a recursive algorithm to find the maximum element in an array of n elements and...
write a recursive algorithm to find the maximum element in an array of n elements and analyze its time efficiency. (I am using c++ programming language)
Given an array A[0 … n-1], where each element of the array represent a vote in...
Given an array A[0 … n-1], where each element of the array represent a vote in the election. Assume that each vote is given as an integer representing the ID of the chosen candidate. Can you determine who wins the election? What is the complexity of your solution? Hint: it is similar to finding the element that is repeated the maximum number of times.
Describe an efficient recursive algorithm for solving the element uniqueness problem
Describe an efficient recursive algorithm for solving the element uniqueness problem, which runs in time that is at most O(n2) in the worst case without using sorting.    
Using Java, Given an array A[0 ... n-1], where each element of the array represent a...
Using Java, Given an array A[0 ... n-1], where each element of the array represent a vote in the election. Assume that each vote is given as an integer representing the ID of the chosen candidate. Can you determine who wins the election? What is the complexity of your solution? Hint: it is similar to finding the element that is repeated the maximum number of times.
You are given an array of n elements, and you notice that some of them are...
You are given an array of n elements, and you notice that some of them are duplicates, that is, they appear more than once in the array. Show how to remove all duplicates from the array in time O( n log2 n ).
Consider sorting n numbers stored in array A by first finding the smallest element of A...
Consider sorting n numbers stored in array A by first finding the smallest element of A and exchanging it with the element in A[1]. Then find the second smallest element of A and exchange it with A[2]. Continue in this manner for the first n-1 elements of A. a) (10 points) This algorithm is known as the selection sort. Write the pseudo code for this algorithm. b) (10 points) What loop invariant does this algorithm maintain? Note: You do not...
. Consider QuickSort on the array A[1:n] and assume that the pivot element x (used to...
. Consider QuickSort on the array A[1:n] and assume that the pivot element x (used to split the array A[lo:hi] into two portions such that all elements in the left portion A[lo:m] are ≤x and all elements in the right portion A[m:hi] are ≥x) is the second element of the array to be split (i. e., A[lo+1]). For a specific value of n at least 13 (your choice), construct an assignment of the numbers 1...n to the n array elements...
ATestforPrimalityisthefollowing: Given an integer n > 1, to test whether n is prime check to see...
ATestforPrimalityisthefollowing: Given an integer n > 1, to test whether n is prime check to see if it is divisible by a prime number less than or equal to it’s square root. If it is not divisible by an of these numbers then it is prime. We will show that this is a valid test. prove ∀n, r, s ∈ N+, r s ≤ n → (r ≤ √n ∨ s ≤ √n) b) Prove ∀ n ∈ N+ ,...
Using c++ to find all the peaks elements in a 2d array, each element has eight...
Using c++ to find all the peaks elements in a 2d array, each element has eight neighbours
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT