Instead you can use hashset or hashmap according to needs…
Or use both vector and HashSet… before pushing integer to vector , check if it exist in hashset or not… and if it does not exist then push it in hashset as well as vector
I would need more details of question to help you more…
This approach will take O(n) instead of brute force O(n^2)
Thank you. Sorry for late reply. But I have been practicing since reading your answer. And it helped me a lot. Practiced over 200+ question since then and got a really awesome way to implement things using hashmap and map as well.
But I wanted to ask, neglecting log(n) vs O(1) access time, where should we prefer Map and where unordered_map?
I usually prefer map only when I need the elements to be sorted… otherwise I mostly go with unordered map…
Further unordered map is Hash map and it has O(n) time complexity in case of hash collisions… But it’s in rare case and it depends on case… According to my experience it doesn’t harm more… but U can always try using same problem with map if u find issue of collisions… I also had read somewhere that it may use more memory…
While map is self balancing tree… Hence it has constant O(logn) complexity…