parsa - 1 year ago 77

C++ Question

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

example:

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

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])
{
count++;
}
else
{
std::cout << v[i-1] << count << std::endl;
count = 0;
}
}
std::cout << v[v.size()-1] << count << std::endl;
```