Do all elements of a heap have to be unique?

Question

In the context of this lesson, do all elements of a heap have to be unique?

Answer

This can depend on your implementation of a heap, but in our implementation, no, the heap elements do not have to be unique. Even with duplicate elements, the heap properties will still hold, and the methods will all work correctly.

In the full implementation of the heap, from exercise 1, we can see that the methods utilize if/else statements when doing things like comparing values. The if statement conditions utilize only > or <, but the else statements will take care of when the values are equal, allowing it to work for duplicate values.