Sina Sameti
Sina Sameti

Reputation: 47

counting multiple Array elements

i wrote a C++ program which shows me the number of repetition of array elements ...my source code is :

#include <iostream>
#include <string>
using namespace std;
int main() {
    int x[20] = {1,1,32,43,54,65,76,76,76,2,12,12,32,43,54,3,3,23,1,43};
    for (int i=0;i<20;i++) {
    }

    for (int i=0;i<20;i++) {
        int count=1;
        for (int j=i+1;j<19;j++) { 
            if (x[i]==x[j]) count++;
        }
        cout<<"The number "<<x[i]<<" is repeated "<<count<<" times"<<"\n";
    }
}

and out put of this is :

The number 1 is repeated 3 times
The number 1 is repeated 2 times
The number 32 is repeated 2 times
The number 43 is repeated 2 times
The number 54 is repeated 2 times
The number 65 is repeated 1 times
The number 76 is repeated 3 times
The number 76 is repeated 2 times
The number 76 is repeated 1 times
The number 2 is repeated 1 times
The number 12 is repeated 2 times
The number 12 is repeated 1 times
The number 32 is repeated 1 times
The number 43 is repeated 1 times
The number 54 is repeated 1 times
The number 3 is repeated 2 times
The number 3 is repeated 1 times
The number 23 is repeated 1 times
The number 1 is repeated 1 times
The number 43 is repeated 1 times

the problem is output shows array element each time but i want that my program just shows the repeated array just for once. and i don't want to define new array .. Anyone have a clue about what's going on ??

Note : that without definition any new array and without sorting program output should be like this :

    The number 1 is repeated 3 times
    The number 32 is repeated 2 times
    The number 43 is repeated 3 times
    The number 54 is repeated 2 times
    The number 65 is repeated 1 times
    The number 76 is repeated 3 times
    The number 2 is repeated 1 times
    The number 12 is repeated 2 times
    The number 3 is repeated 2 times
    The number 23 is repeated 1 times

Upvotes: 2

Views: 462

Answers (3)

Felix Glas
Felix Glas

Reputation: 15524

This is a reduce operation. C++ have a left-fold operation called std::accumulate in algorithms. Feed it with a std::map to create a count record.

auto count_map = std::accumulate(std::begin(x), std::end(x),
                                 std::map<int, int>{},
                                 [] (auto& m, int val) -> decltype(m) {
    ++m[val];
    return m;
});

// Output result
for (auto&& [val, count] : count_map) {
    std::cout << "Value: " << val << " - Count: " << count << std::endl;
}
# Output:
Value: 1 - Count: 3
Value: 2 - Count: 1
Value: 3 - Count: 2
Value: 12 - Count: 2
Value: 23 - Count: 1
Value: 32 - Count: 2
Value: 43 - Count: 3
Value: 54 - Count: 2
Value: 65 - Count: 1
Value: 76 - Count: 3

Upvotes: 1

lcs
lcs

Reputation: 4245

You can use a map to count your elements, which satisfies your requirement that you do not create a new array.

std::map<int, int> counts;
for(auto&& elem : x)
{
    counts[elem]++;
}

for(auto&& item : counts)
{
    std::cout << "The number " << item.first << " is repeated " << item.second << " times; 
}

Upvotes: 4

Jarod42
Jarod42

Reputation: 217265

Without extra structures, you may simply do:

#include <algorithm>
#include <iostream>
#include <string>

int main() {
    const int x[20] = {1,1,32,43,54,65,76,76,76,2,12,12,32,43,54,3,3,23,1,43};

    for (int i=0;i<20;i++) {
        if (std::find(x, x + i, x[i]) != x + i) {
            continue;
        }
        const auto count = std::count(x + i, x + 20, x[i]);
        std::cout << "The number " << x[i] << " is repeated " << count << " times\n";
    }
}

Demo

Upvotes: 0

Related Questions