Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8056725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:48:20+00:00 2026-06-05T08:48:20+00:00

Is there any elegant way to subtract std::vector s, that contain duplicated elements? Example:

  • 0

Is there any elegant way to subtract std::vectors, that contain duplicated elements?


Example:

v1 = { 3, 1, 2, 1, 2, 2 }
v2 = { 2, 4, 3, 3, 3 }
result1 = ??( v1, v2 )
result2 = ??( v2, v1 )

and I want the result to be:

result1 = { 1, 1 }
result2 = { 4 }

My current (and very slow) solution:

1) sort v1 and v2
2) use std::unique_copy to v1_uniq, v2_uniq
3) intersect the new vectors with std::set_intersection
4) iterate over v1 and v2 and remove all elements, that are in the intersection 3)

My other idea is:

1) sort v1 and v2
2) iterate over v1 and v2 and remove duplicates in parallel 

But this is kinda error-prone doesn’t look elegant to me.

Any other ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T08:48:24+00:00Added an answer on June 5, 2026 at 8:48 am

    You could use std::copy_if with a unary predicate that checks whether the element is in the second vector. Or, if you don’t have C++11 support, use std::remove_copy_if with the predicate’s logic suitably changed.

    For the unary predicate:

    struct Foo {
    
      Foo(const std::vector& v) : v_(v) {}
      bool operator() (int i) const {
        // return true if i is in v_
      }
      const std::vector<int>& v_;
    
    };
    

    which can be instantiated like this:

    Foo f(v2);
    

    You could modify the functor to keep a sorted version of the reference vector, with unique entries to allow to do a binary search, but the general idea is the same.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way that I can create a new delegate type based on
Is there an elegant way to make a program detect a new computer that
This is probably a bit low question, but is there any elegant way, how
Is there any elegant way of applying a certain style to all <input type=text>
Is there any elegant way to exploit the correct spacing feature of print numpy.array
Hi Is there any elegant way of combining 'like' and 'or' when i'm using
Is there any elegant way to Cut off support for older iOS operating system
Is there any elegant way of turning [$(div), $(span), $(li)] into $(div, span, li)
Is there any elegant way of splitting a list/dict into two lists/dicts in python,
In java is there any elegant way to compare two different StringBuilders and copy

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.