I’m having trouble of thinking up an efficient algorithm for a map vector issue I have.
Say I have a map<int , vector <int> > and i would like to find out how many times an integer occurs in all of vector of the map, and if it occurs over a predefined amount remove it’s value from the rest of vectors (hopefully that makes sense), here is a quick example:
key values
1 – <2,3,4,4,5>
2 – <2,3,3,4,5>
3- <2,3,3,4,6>
In this case say I wanted to remove 4 from all the vector if it has occurred more than three times. The resulting map would look like this:
1 – <2,3,4,4,5>
2 – <2,3,3,4,5>
3- <2,3,3,DEL,6>
I’m looking for an efficient algorithm for this issue and was just wondering if anyone had any ideas. (I’m working in C++ , but I know java or pseudo code is cool).
Thanks for any help
Side Note the vectors are not sorted in real life just in this example.
Since the vectors aren’t sorted the only way to do this is to iterate through all the items of all the vectors, keep track of how many have been found, and clean up the vector as appropriate. I think it would be straightforward to do this with an externally stateful predicate and the remove-erase idiom.
Given your precise needs and container use however, alternate approaches might be available. For example if the
vectordoesn’t actually need to be indexable you could use amultisetinstead to keep the items sorted and provide easy counting and removal.