# C++ remove duplicates from vector without sorting

1 vote
995 views

## Problem:

Hello Kodlogs,

Let me thank you guys for being awesome. You guys are friendly and helpful. I posted a problem and got a solution with an explanation. I have got a new problem today while I was trying to remove duplicates from a vector of C++ language. Let me put the problem this way. If I have a vector

``v = { 5, 2, 1, 3, 4, 2, 2, 4, 5, 5, 6 }``

and I want to remove all the duplicates from it and the final output should be

``5 2 1 3 4 6``

How can I remove those duplicates from the vector, any idea? Please share your thoughts on it. Thanks again for your valuable time.

1 vote

## Solution:

The idea behind the solution is pretty simple. Just run a simple iteration through the vales of your vector and find the duplicate values. Once you find the duplicate values, just delete values. We can either write our own routine for this or use the std::remove algorithm that makes our code elegant.

``````#include <iostream>
#include <vector>
#include <algorithm>

void remove(std::vector<int> &v){
auto end = v.end();
for (auto i = v.begin(); i != end; ++i) {
end = std::remove(i + 1, end, *i);
}
v.erase(end, v.end());
}

int main(){
std::vector<int> v = { 5, 2, 1, 3, 4, 2, 2, 4, 5, 5, 6 };
remove(v);
for (auto i = v.cbegin(); i != v.cend(); ++i)
std::cout << *i << ' ';
return 0;
}``````

This program should print the result as you were asking, and the vector v would be

``5 2 1 3 4 6 ``

Keep posting. Thanks

The simple solution is to iterate the vector and for each statement, we delete all its duplications from the vector if present. You can either write your own routine for this or use the std:: remove algorithm that makes the code elegant.

# Time complexity:

This approach takes the constant space but runs in O(n^2) time.

## Example code:

You can use the additional set to mark the unique values.

``````#include <iostream>

#include <algorithm>

#include <vector>

template <typename ForwardIterator>

ForwardIterator remove_duplicates( ForwardIterator first, ForwardIterator last )

{

auto new_last = first;
for ( auto current = first; current != last; ++current )

{

if ( std::find( first, new_last, *current ) == new_last )

{

if ( new_last != current ) *new_last = *current;

++new_last;

}

}

return new_last;

}

int main()

{

std::vector<int> v = { 1, 3, 2, 1, 4, 3 };
for ( int x : v ) std::cout << x << ' ';

std::cout << std::endl;
v.erase( remove_duplicates( v.begin(), v.end() ), v.end() );

for ( int x : v ) std::cout << x << ' ';

std::cout << std::endl;
return 0;

}``````

## Output:

The output of the above code is ;

``````1 3 2 1 4 3

1 3 2 4``````

## Related questions

1 answer 20 views
20 views
Problem: I need to take a C++ vector with potentially a lot of elements, erase duplicates, and sort it. I currently have the below code, but it doesn't work. vec.erase( std::unique(vec.begin(), vec.end()), vec.end()); std::sort(vec.begin(), vec.end());
1 answer 2 views
2 views
Problem: Need how to fix this? Thanks Remove value from vector c++ .
1 answer 3 views
3 views
Problem: Any Python expert , please help ? remove duplicates from array c++
1 answer 7 views
7 views
Problem: I have a CSV file with multiple entries. Each entry represents a person. It has first_name, last_nameand other details. My goal is to remove duplicates from this data, but in a sensible way. The records come from different sources, and duplicates may contain ... number of records that I need to process is about 100 million. I am considering using pyspark, but any technology is welcome.
1 answer 8 views
8 views
Problem: How to I Solve this: remove duplicates from list keep orde HELP! I need help understanding this: remove duplicates from list keep order Can someone please help solve this, I am new in python().
1 answer 5 views
5 views
Problem: The duplicate occurrences of values from the following list (array) need to be removed: 7, 9, 3, 1, 7, 8, 9, 1, 9, 11, 10 I have written the following code in an attempt to do the job: class Main { public static void main (String[] args) ... array. I do not understand what I am doing wrong and how to get the desired result without worrying about the order of the values. Any suggestions?
1 answer 13 views
13 views
Problem: have a list of lists in Python: k = [[1, 2], [4], [5, 6, 2], [1, 2], [3], [4]] And I want to remove duplicate elements from it. Was if it a normal list not of lists I could used set. But unfortunate that list is not hashable and can't ... [3], [4]] I don't care about preserve order. Note: this question is similar but not quite what I need. Searched SO but didn't find exact duplicate.