C++ 计算数组中元素重复的次数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34820275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:25:57  来源:igfitidea点击:

Count how many times elements in an array are repeated

c++

提问by Mahmoud Nabil

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.

我正在尝试编写的程序允许我输入 10 个数字,它应该告诉我数字 X 重复了 X 次等等。

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}

例如...{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

数字 1 重复了 4 次

数字 1 重复了 3 次

数字 1 重复了 2 次

数字 1 重复 1 次

数字 4 重复了 3 次

数字 6 重复 1 次

数字 4 重复了 2 次

数字 7 重复 1 次

数字 4 重复 1 次

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";
    }
}

回答by saadtaame

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

您的代码的问题在于您重新处理了已经处理过的数字。因此,如果1在位置 0 处发生了 ,而1在位置 5 处又发生了一次,那么1当您到达循环中的位置时,您将再次处理位置 5。

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.

所以你需要一种方法来决定一个数字是否已经被处理过。一个简单的方法是添加第二个数组(最初所有值都设置为 0),每当您处理一个数字时,您就标记该元素出现的所有位置。现在在处理一个元素之前,你检查它是否已经被处理过,如果是这样的话,什么都不做。

Also, try to indent your code properly :)

另外,请尝试正确缩进您的代码:)

C++ Code:

C++代码:

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;
}

回答by andand

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

这是一个相当简单的实现,使用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 autoand it should work with C++98.

使用 C++14 标准编译,但它也应该适用于 C++11。摆脱向量初始值设定项和使用,auto它应该适用于 C++98。

回答by Shramana Roy

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

回答by Abraham Hernandez

Pretty simple using map!

使用非常简单map

See the Repl.it

参见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

回答by chandresh

#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

6

enter element:6

输入元素:6

enter element:5

输入元素:5

enter element:5

输入元素:5

enter element:6

输入元素:6

enter element:2

输入元素:2

enter element:3

输入元素:3

2is repeat:1time

2是重复:1次

3is repeat:1time

3是重复:1次

5is repeat:2time

5是重复:2次

6is repeat:2time

6 次重复:2 次