The question is a little complex. The problem here is to get rid of duplicates and save the unique elements of array into another array with their original sequence.
As Thomas suggested in a comment, if each element of the array is guaranteed to be from a limited set of values (such as a char
) you can achieve this in O(n)
time.
bool
(or int
if your compiler doesn't support bool
) or however many different discrete values could possibly be in the array. Initialize all the values to false
.bool
array is false
, add it to the output array and set the bool
array value to true
. Otherwise, do nothing.