Describe an algorithm (either in pseudocode or in text) that, given an array B of n integers, determines whether or not there exists a duplicate in B. For instance, if the input is B: 5,2,3,5,1,7,9,5,then the algorithm has to return YES, because of the two 5’s in B (Actually, there are three 5’s in B, but two issufficient for the algorithm to return YES.) But, if the input is, for instance B: 6,9,1,0,4,7, then the algorithm has to return NO, because all numbers in B are unique and thus there is no duplicate in B.Also, analyze the running time and space complexity of your algorithm in the worst-case. Please be as precise as you can in both describing your algorithm and its time and space complexity analysis.[
Note 1.Your algorithm is expected to be a time-efficient algorithm.
Transcribed text From Image:
Expert Chegg Question Answer:
1. Create an array B of n integers.
2. Create a Hshset and start inserting elements to it one by one. As we know set data structure allows insertion of only unique elements.
3. Comapre the size of array with the size of Hashset, if both are equal then return “YES” and if the size doesn’t match return “NO”.
The time complexity of the above operation will be O(n) where n is the number of elements in the array i.e. linear time complexity. As the array is traversed once inorder to access and insert the accessed elements to the Hashset. Traversal of array always takes linear time which is order of n complexity.
The space complexity is also going to be of O(n) as creating a Hashset for n integers can be of n block size. This operation also takes linear time complexity. if no extra space is created during the execution of the program then space complexity will be always constant i.e. O(1).
HOPE YOU LIKE THE SOLUTION!!!!!