Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Consider this data

Customer      Account
1                10
1                11
2                13
3                11
4                14
3                10
4                13


Write a function that return customers that have perfect match account. In the above, output should be 1,3 because both customer 1 and 3 have account 10 & 13 Customer 2 only has a match of account 13 with customer 4. But since customer 4 also has account 14, it’s not a perfect match.

What's the best way to achieve this in C++, especially when the size of the data is big in the thousands? I've tried with map/dictionary, set but it's ugly.

Thanks in advance

What I have tried:

I've tried with map/dictionary, set but it's ugly.
Posted
Updated 11-Jan-21 20:33pm
v2
Comments
PIEBALDconsult 11-Jan-21 19:42pm    
Not enough information, and you'll likely do better with a database.
roger.t.jefferson 11-Jan-21 19:48pm    
Here is what I've tried.
First, I iterate customers to find out what accounts each customer has. I simply add the account to the set as I find it.

Customer
std::set<account>

So, once I finish the iteration, all customers have their account recorded.

I then iterate the customer again to build this map/dictionary structure

std::map<std::set<account>, std::vector<customer>>

Once this step is done, I have something like this:

key (as set<account>) value (as vector<customer>)
{10,11} {1,3}
{13} {2,4}
{14} {14}

If I use database, any suggestion or hint on what to do?
PIEBALDconsult 11-Jan-21 19:55pm    
What defines a "perfect match account" ?
roger.t.jefferson 11-Jan-21 20:40pm    
Customer 1 & 3 have account 10,13. This is a perfect match.

Customer 2 & 4 have account 13. But since customer 4 also has account 13, customer 2 & 4 are NOT a perfect match
Richard Deeming 12-Jan-21 11:55am    
In the sample data you've provided, customers 1 and 3 have accounts 10 and 11. Neither has account 13.

1 solution

You could reverse the logic and build, for each customer, a vector of accounts, then perfect matches are obtained comparaing the vectors of accounts themselves.
 
Share this answer
 
Comments
KarstenK 12-Jan-21 11:15am    
Fully agree and good idea would be to sort that in an array with that vector, so an easy and flat access is possible.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900