parsa parsa - 1 year ago 101
C++ Question

finding the number of same integers in vector

I have a vector of integers . I want to count same integers in vector .I need a simple algorithm for it .but without using too much headers or built in functions,just by a simple algorithm .
thanks so much

std::v={1,1,1,2,2,3} 1:3----2:2----3:1

yd1 yd1
Answer Source

Sort them, and then count every change in following digits.

optional: save the count to an output array.

try after sort:

int count = 1;
for(int i = 1; i < v.size(); i++)
    if(v[i-1] == v[i])
        std::cout << v[i-1] << count << std::endl;
        count = 0;
std::cout << v[v.size()-1] << count << std::endl;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download