Count how many times elements in an array are repe

2020-04-01 08:14发布

The program I'm trying to write allows me to enter 10 numbers and it should get tell me Number X is repeated X times and so on.

I've been trying this but the problem is I get the result as follows:

For example...{1,1,1,1,4,6,4,7,4}

The number 1 is repeated 4 times

The number 1 is repeated 3 times

The number 1 is repeated 2 times

The number 1 is repeated 1 times

The number 4 is repeated 3 times

The number 6 is repeated 1 times

The number 4 is repeated 2 times

The number 7 is repeated 1 times

The number 4 is repeated 1 times

The problem is that it checks the next number with the following numbers without skipping it, or without knowing it has written it before

#include <iostream>
#include <string>
using namespace std;
int main() {
    int x[10];
    for (int i=0;i<10;i++) {
        cin>>x[i];
    }

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

标签: c++
5条回答
老娘就宠你
2楼-- · 2020-04-01 08:31

The most effective way I have recently come across with this...

#include<iostream>
#include<cstring>
using namespace std;

int main()
{
int array[10]={1,1,1,1,4,6,4,7,4};
int a[100];
memset(a,0,sizeof(a));
for(int i=0; i<sizeof(array)/sizeof(array[0]); i++)
{
    a[array[i]]++;
}
for(int i=1; i<sizeof(a)/sizeof(a[0]); i++)
{
    if(a[i]>0)
    {
        cout<<"The number "<<i<<"is repeated "<<a[i]<<" times"<<"\n";
    }

}

OUTPUT:

The number 1 is repeated 4 times
The number 4 is repeated 3 times
The number 6 is repeated 1 times
The number 7 is repeated 1 times
查看更多
ら.Afraid
3楼-- · 2020-04-01 08:35

Here's a fairly simple implementation using std::map.

#include <map>
#include <vector>
#include <cstdlib>
#include <iostream>

std::map<int, unsigned int> counter(const std::vector<int>& vals) {
    std::map<int, unsigned int> rv;

    for (auto val = vals.begin(); val != vals.end(); ++val) {
        rv[*val]++;
    }

    return rv;
}

void display(const std::map<int, unsigned int>& counts) {
    for (auto count = counts.begin(); count != counts.end(); ++count) {
        std::cout << "Value " << count->first << " has count "
                  << count->second << std::endl;
    }
}

int main(int argc, char** argv) {
    std::vector<int> mem = {1, 1, 1, 1, 4, 6, 4, 7, 4};
    display(counter(mem));

    return 0;
}

Output:

Value 1 has count 4
Value 4 has count 3
Value 6 has count 1
Value 7 has count 1

Compiled using the C++14 standard, but it should also work with C++11. Get rid of the vector initializer and use of auto and it should work with C++98.

查看更多
萌系小妹纸
4楼-- · 2020-04-01 08:42
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cout<<"enter length of array:"<<endl;
    cin>>n;
    int arr[n];
    for(int i=0;i<n;i++)
    {
        cout<<"enter element:";
        cin>>arr[i];

    }
    sort(arr,arr+n);
    /*this is for sort the array so we can find maximum element form user input and 
   using this element we make one array of that size
   */
    int m=arr[n-1];
    m++;
    int a[m];


    for(int i=0;i<m;i++)
    {
        a[i]=0;
    }

    for(int i=0;i<n;i++)
    {

     a[arr[i]]++;
    }
   cout<<endl;
        for(int i=0;i<m;i++)
        {
            if(a[i]>0)
                cout<<i<<"is repeat:"<<a[i]<<"time"<<endl;


        }


}

output is like this:

enter length of array:

6

enter element:6

enter element:5

enter element:5

enter element:6

enter element:2

enter element:3

2is repeat:1time

3is repeat:1time

5is repeat:2time

6is repeat:2time

查看更多
爷、活的狠高调
5楼-- · 2020-04-01 08:44

The problem with your code is that you re-process numbers that you've already processed. So if there is an occurrence of 1 at position 0 and another occurrence of 1 at position 5, then you will process the 1 at position 5 again when you get there in the loop.

So you need a way to decide if a number has been processed already or not. An easy way is to add a second array (initially all values are set to 0) and whenever you process a number you mark all positions where that element occurs. Now before processing an element you check if it's been processed already and do nothing if that's the case.

Also, try to indent your code properly :)

C++ Code:

int main( void ) {
    const int N = 10;

    int A[N];
    for(int i = 0; i < N; i++)
        cin >> A[i];

    int seen[N];
    for(int i = 0; i < N; i++)
        seen[i] = 0;

    for(int i = 0; i < N; i++) {
        if(seen[i] == 0) {
            int count = 0;
            for(int j = i; j < N; j++)
                if(A[j] == A[i]) {
                    count += 1;
                    seen[j] = 1;
                }
            cout << A[i] << " occurs " << count << " times" << endl;
        }
    }

    return 0;
}
查看更多
家丑人穷心不美
6楼-- · 2020-04-01 08:47

Pretty simple using map!

See the Repl.it

#include <iostream>
#include <map>

int main()
{
    int foo[]{1,1,1,1,4,6,4,7,4};
    std::map<int, int> bar;

    for (auto const &f : foo)
        bar[f]++;

    for (auto const &b : bar)
        std::cout << "The number " << b.first 
                  << "is repeated " << b.second 
                  << "times\n";
}

Expected output:

The number 1 is repeated 4 times
The number 4 is repeated 3 times
The number 6 is repeated 1 times
The number 7 is repeated 1 times
查看更多
登录 后发表回答