INTUITION:

APPROACH:

Time Complexity:O(N)

Space Complexity:O(N) where N represents the size of HashSet where we are storing our elements.

Here’s the code: