How Time Complexity is O(n)?

It should be nlogn as we are using set.

Yes, it should be O(nlogn)

Sorry, it is given unordered_set and its time complexity is O(1)