Hacker-M's blog

By Hacker-M, 2 years ago, In English

Hi, Which one of the following ways is better to store the non-duplicate numbers?

1- Using sort and unique in std :: vector in C++ like this :

sort(a.begin(), a.end());
a.erase(unique(a.begin(), a.end()), a.end());

2- Using std :: set in C++

I know set's Order is around $$$O(nlogn)$$$ with a little bad coefficients But I don't know what's the order of using vector and unique, So If it is possible tell me which one is faster and better, Thanks :).

  • Vote: I like it
  • -13
  • Vote: I do not like it

| Write comment?
»
2 years ago, # |
  Vote: I like it 0 Vote: I do not like it

I wrote three programs that sort an array and remove equal elements.

std::sort Time: 4.80 s

std::stable_sort Time: 5.20 s

std::set Time: 15.43 s

P.S. Each of these programs uses a random number generator with seed 238. I tried running programs with other seeds and the results were almost the same.