We know set is an unordered collection.
Exercise How should we implement the iterator for Set ADT?
A) Simply iterate in the same order the elements have been added.
B) To ensure this is an unordered collection, we must iterate over the elements in a random order.
C) Iterate over the elements from head
to tail
(from index 0
to numElements - 1
in array).
Solution
The correct answer is C. It is the cheapest strategy.
The statement that “set is an unordered collection” implies that a client shall not expect the iteration is done in any particular order. We don’t need to go out of our way to ensure an un-orderly iteration!