计算一个值在数组中出现的次数 [英] Counting the number of times a value appears in an array

查看:105
本文介绍了计算一个值在数组中出现的次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那么在C#中创建一个循环的好简单算法是什么,每次在数组中出现某个值时,都会在另一个数组中的计数器上加1?

So what's a good, simple algorithm to create a loop in C# where every time a certain value appears in an array it adds 1 to a counter in another array?

例如,我有这个:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication22
{
    class Program
    {
        const int SIZE = 12;

        static void Main(string[] args)
        {
            int[] numbers = new int[SIZE] {5, 5, 5, 7, 7, 7, 9, 7, 9, 9, 9, 1};
           string[] letters = new string[SIZE] { "m", "m", "s", "m", "s", "s", "s", "m", "s", "s", "s", "s" };
            int[] values = new int[SIZE] {15, 22, 67, 45, 12, 21, 24, 51, 90, 60, 50, 44};
            string[] status = new string[SIZE] { "f", "m", "f", "a", "m", "f", "f", "f", "m", "f", "m", "f" };

            int[] Count = new int[4];
            int x = 0;
            int i = 0;

            for (i = 0; i < SIZE - 1; i++)
            {
                if (numbers[i] > 0 && numbers[i] < SIZE)
                {
                    x = Count[i];
                    Count[x]++;
                }
            }

            for (i = 0; i < 4; i++)
            {
                Console.WriteLine("{0}", Count[4]);
            }
        }
    }
}

我只计算4个数字出现在数字数组中的次数.有人建议我在第一个循环中使用该方法,但它似乎不起作用,并会产生一个错误,指出索引在数组中超出范围.我想显示每个数字(5、7、9和1)出现在4行中的次数.

I am only counting the number of times 4 numbers appear in the numbers array. Someone suggested I use the method in the first loop but it doesn't seem to be working and creates an error that the index is out of bounds in the array. I want to display the number of times each of those numbers(5, 7,9 and 1) appear in 4 rows.

不使用LINQ或任何其他精美的东西,例如Dictionary或其他任何东西.

Without using LINQ or any other fancy thing like Dictionary or whatever.

推荐答案

由于本节的原因,您获得了超出范围的错误:

You're getting an index out of bounds error because of this section:

for (i = 0; i < SIZE - 1; i++)
{
    if (numbers[i] > 0 && numbers[i] < SIZE)
    {
        x = Count[i];

请注意,当Count仅具有4的大小时,您正在遍历0SIZE - 1(11).

Notice that you're iterating through 0 to SIZE - 1 (11) when Count only has a size of 4.

不过,您可以使用LINQ轻松完成此任务.

You can do this task pretty easily with LINQ though.

int[] numbers = new int[SIZE] { 5, 5, 5, 7, 7, 7, 9, 7, 9, 9, 9, 1 };

var count = numbers
    .GroupBy(e => e)
    .Where(e => e.Count() == 4)
    .Select(e => e.First());

因此它将数字按其值分组,然后我们将列表精简为仅包含4组,然后选择每个组中的第一个,并留下一个int s的集合.

So it groups the numbers by their value, we then refine the list to only include groups of 4, then select the first of each to be left with a collection of ints.

这是一个非基于Linq的解决方案,使用字典来存储数字计数.

Here is a non-LINQ based solution using a Dictionary to store the count of numbers.

int[] numbers = new int[SIZE] { 5, 5, 5, 7, 7, 7, 9, 7, 9, 9, 9, 1 };
var dictionary = new Dictionary<int, int>();
var numbersWithFour = new List<int>();

foreach (var number in numbers)
{
    if (dictionary.ContainsKey(number))
        dictionary[number]++;
    else
        dictionary.Add(number, 1);
}

foreach (var val in dictionary)
{
    if (val.Value == 4)
    {
        numbersWithFour.Add(val.Key);
    }
}


对程序进行一些修改,就可以得到一些结果.


With a little modification to your program you can get some results.

int[] numbers = new int[SIZE] { 5, 5, 5, 7, 7, 7, 9, 7, 9, 9, 9, 1 };
string[] letters = new string[SIZE] { "m", "m", "s", "m", "s", "s", "s", "m", "s", "s", "s", "s" };
int[] values = new int[SIZE] { 15, 22, 67, 45, 12, 21, 24, 51, 90, 60, 50, 44 };
string[] status = new string[SIZE] { "f", "m", "f", "a", "m", "f", "f", "f", "m", "f", "m", "f" };

// Set the size of Count to maximum value in numbers + 1
int[] Count = new int[9 + 1];
int x = 0;
int i = 0;

for (i = 0; i < SIZE - 1; i++)
{
    if (numbers[i] > 0 && numbers[i] < SIZE)
    {
        // Use value from numbers as the index for Count and increment the count
        Count[numbers[i]]++;
    }
}

for (i = 0; i < Count.Length; i++)
{
    // Check all values in Count, printing the ones where the count is 4
    if (Count[i] == 4)
        Console.WriteLine("{0}", i);
}

输出:

7
9

这篇关于计算一个值在数组中出现的次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆