Push unique array?

Please help me in this;

Let’s say I have n inputs a_1, a_2, a_3, a_4, a_5, …, a_n

I have a vector vector<unsigned long long int> v

What should I do if I do not want to input a_i in v is value of a_i already exist in v

Problem is n is very large and elements can have a range of 0-10^9

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)

2 Likes

never even thought of combining vector and hash_set, usually for this ocassion I just use hash_set

1 Like

Yeah but sometimes we need both for easy traversing or for any other purpose… hence I suggested using both…

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…

Welcome and np for late reply…

Btw in which year r u studying rn ? I completed my 1st year…