how to prevent duplicate when compare 2 string vector.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <vector> 
#include <random>
#include <algorithm>
#include <iterator> 
#include <ctime> 
#include <set> 
using namespace std; 


int main() 
{
    vector<string> currentUser = {"michael" , "john" , "jordan", "xander", "hard"} ;
    vector<string> newUser = {"jordan", "michelle", "john", "jake", "james"};

    for(int i = 0; i < currentUser.size(); i++) 
    {
        for(int j = 0; j < newUser.size(); j++) 
        {
            if(newUser[j] == currentUser[i]) 
            {
                cout << newUser[j] << " is not available!!!!!!!!!!!!!!!!" <<  endl; 
                break;
            }
            else
            {
                //how to prevent duplicate? 
                cout << newUser[j] << " is available" << endl; 
            }
        }
    }
    
}
 


how to rewrite the code such that no duplicate will be printed?
I want to print both the matching and non matching string but there is always duplicate. how do I prevent duplicates from appearing?
thank you.
Last edited on
You already include set, so why not use it?
https://en.cppreference.com/w/cpp/container/set

In particular, https://en.cppreference.com/w/cpp/container/set/find

You store the current users in a set, then use find with each prospective new user. If you don't find the new user, add them to the set.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <vector>
#include <set>

int main() {
	std::set<std::string> currentUser{ "michael" , "john" , "jordan", "xander", "hard" };
	const std::vector<std::string> newUser { "jordan", "michelle", "john", "jake", "james" };

	for (const auto& n : newUser)
		if (!currentUser.insert(n).second)
				std::cout << n << " is not available!!!!!!!!!!!!!!!!\n";
			else
				std::cout << n << " is available\n";
}



jordan is not available!!!!!!!!!!!!!!!!
michelle is available
john is not available!!!!!!!!!!!!!!!!
jake is available
james is available

You have a loop that always prints something:
1
2
3
4
5
6
7
for(int i = 0; i < currentUser.size(); i++) 
{
    for(int j = 0; j < newUser.size(); j++) 
    {
        // print a line
    }
}

That is, it prints currentUser.size() * newUser.size() lines.

Just with
1
2
vector<string> currentUser {"michael" , "john" , "jordan", "xander", "hard"} ;
vector<string> newUser {"buffy"};

the "buffy is available" prints five (currentUser.size()) times.

You apparently want to print each new user once. That is, repeat the outer loop newUser.size() times:
1
2
3
4
5
6
for( const auto& name : newUser ) 
{
  // test whether currentUser contains name

  // print appropriate message
}



The seeplus version does an extra step, it weeds out duplicates from newUser too.
With newUser { "jordan", "jake", "michelle", "john", "jake", "james" }; it prints:
jordan is not available!!!!!!!!!!!!!!!!
jake is available
michelle is available
john is not available!!!!!!!!!!!!!!!!
jake is not available!!!!!!!!!!!!!!!!
james is available

Which is probably logically correct thing to do, isn't it?
@seeplus that's a very short and sweet piece of code. I didn't know I could use insert().second to get the boolean value. I learn something new today :)
thank you everyone.
Last edited on
It has been a while since I have used c++.
The forum format has changed since my last visit.
So take this with a pinch of salt.
It is very cheesy.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71

#include <iostream>
#include <vector> 

using namespace std; 

int Is_in( string n,vector<string> s)
{
      for (int i = 0;i < s.size();i++)
            if (n==s[i])  return 1;
      return 0;
}

void add_to_set(string n,vector<string>&s)
{
      int b=0;
     b= Is_in(n,s);
      if (b ==1) return;
       s.push_back (n);
}

void setunion(vector<string>s,vector<string>t,vector<string> &u)
{
int b=0;
string k="";
u.resize(s.size());
      for (int i = 0;i<s.size();i++)
            u[i]=s[i];
      for (int i=0;i<t.size();i++) 
      {
             k=t[i];
            b=Is_in(k,u) ; 
            if  (b == 0)
            add_to_set(t[i],u);
  }
}


void setintersect(vector<string> s,vector<string>t,vector<string>&u)
{
      u.resize(0);
      string k="";
      int b =0;
      for (int i=0;i<s.size();i++)
      {
             k=s[i];
            b=Is_in(k,t);
            if (b==1) add_to_set(k,u);
      }
}

int main()
{
 vector<string> u;
 vector<string> currentUser = {"michael" , "john" , "jordan", "xander", "hard"} ;
 vector<string> newUser = {"jordan", "michelle", "john", "jake", "james"};
 setunion(currentUser,newUser,u);
    for (int i=0;i<u.size();i++)
 cout<<u[i]<<endl;
 cout<<endl<<"duplicates:"<<endl;	
 setintersect(currentUser,newUser,u);
 	for (int i=0;i<u.size();i++)
 cout<<u[i]<<endl;
 cout <<endl<<"Press return to end . . ."<<endl; 
 	
	 cin.get();
 
}


 
Last edited on
Topic archived. No new replies allowed.