• Register
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.

12 7 7
15,250 points

Please log in or register to answer this question.

2 Answers

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

13 9 6
94,260 points
0 votes

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

 

11 5 2
3,890 points

Related questions

0 votes
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());
asked Dec 15, 2020 Mashhoodch 13k points
0 votes
1 answer 2 views
2 views
Problem: Need how to fix this? Thanks Remove value from vector c++ .
asked Mar 20 PkGuy 14k points
0 votes
1 answer 3 views
3 views
Problem: Any Python expert , please help ? remove duplicates from array c++
asked Mar 20 PkGuy 14k points
0 votes
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.
asked Mar 21 sasha 16.2k points
0 votes
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().
asked Mar 2 Mashhoodch 13k points
0 votes
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?
asked Feb 27 Code Learner 9.5k points
0 votes
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.
asked Feb 19 Mashhoodch 13k points
0 votes
1 answer 21 views
21 views
Problem: I have following list that contains duplicate entries: America Britain England America Pakistan America Pakistan Britain Europe I want to remove duplicates from this list. How can I do this?
asked Oct 23, 2020 Code Learner 9.5k points
1 vote
1 answer 31 views
31 views
Problem: Hello guys, I found python list is fun. I was doing some operation with a python list. Let me say, I have a python list looks: is_a_list = [1, 5, 3, 6, 3, 5, 6, 1] with mixed of duplicate values in it. Now, I am willing to remove all the duplicate elements from the list. How do you do that in an easiest and efficient way? I am eagerly waiting for your answers. Thanks.
asked Jun 25, 2020 adamSw 11.3k points
0 votes
1 answer 3 views
3 views
Problem I am trying to remove an element from vector in c++, but everytime something is showing error, someone can help me how i will do it?
asked Apr 9 Subhendu 4.5k points