Printing the Unique Elements
(Code 5.3)
• Given an array A[0…n-1] that may have elements
appearing more than once, we could use the hash table
to store the unique elements and print them.
• For every element A[i], with 0 ≤ i ≤ n-1, we store the
element A[i] in the hash table the first time we come
across it as well as print it.
• Hence, for every element A[i], with 0 ≤ i ≤ n-1, we check
if A[i] is already in the hash table.
– If A[i] is not already in the hash table, it implies A[i] has not been
seen before: so, we print it out as well as insert in the hash table.
– If A[i] is already in the hash table, it implies we have already
printed it out and should not be printed again.
• The time complexity of the algorithm is dependent on the
time to check whether a particular element is in the hash
table or not for all values of the array index. If this could
be done in Θ(1) time per element, the asymptotic time
complexity of the algorithm is Θ(n).