dimanche 18 novembre 2018

Count item repetitions within a container

How can I count item repetitions within a container in C++?

The problem goes like this: I have a vector with many items on it, how can I know the number of repetitions each element has?

I've seen solutions where you have to create another vector (let's call this countVector) with the value and repetitions then loop through the vector checking if the value hasn't been added to the countVector if the value has been added then proceed to increase the repetitions by 1 but if not then add the new value and set repetitions to 1.

let's be fair, this implementation is an overkill, takes two loops, one to go through the items vector and another to check if the value has been added.

is there a more efficient way to do this?

Aucun commentaire:

Enregistrer un commentaire