Erase and keep only Unique elements from not sorted data | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Erase and keep only Unique elements from not sorted data

Hi Please refer code below: I have tried to remove all repeated numbers from not sorted input data. It is inplace operation and I belive time complexity is O(N) where as space complexity is O(N) for storing data into unordered map... Is time and space complexity correct? Also is it possible to get better solution from this for non sorted data ? If yes , please suggest. https://code.sololearn.com/ctapIM5P1OzC/?ref=app

9th Feb 2022, 10:26 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
4 Answers
+ 5
You might wanna look at std::unique () to achive this. Or just go through different implementations of it in case you don't want to use the function as it is and just want some inspiration to maybe improve your implementation. ----- https://en.cppreference.com/w/cpp/algorithm/unique
10th Feb 2022, 1:37 AM
Arsenic
Arsenic - avatar
+ 3
Ketan Lalcheta, theoretically, a hash map could improve on your complexity score. See if unordered_map from the STL could handle things better and faster for you in one pass at roughly O(1) lookup/storage.
10th Feb 2022, 1:28 AM
Brian
Brian - avatar
+ 1
I tried to go through this link... It seems unique from stl works only for sorted data only .. Right ?
10th Feb 2022, 4:16 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
- 2
Hi the answer is 5.0 is it correct?!
11th Feb 2022, 6:06 PM
salarieditor
salarieditor - avatar