繁体   English   中英

如何检查一个值在数组中出现多少次?

[英]How to check how many times a value appears in an array?

所以这就是我希望输出看起来像的样子:

How many numbers? 10
Give number 1: 1
Give number 2: 3
Give number 3: 1
Give number 4: 3
Give number 5: 4
Give number 6: 6
Give number 7: 4
Give number 8: 8
Give number 9: 2
Give number 10: 1
Number 1 appeared 3 times
Number 2 appeared 1 times
Number 3 appeared 2 times
Number 4 appeared 2 times
Number 6 appeared 1 times
Number 8 appeared 1 times

关键是,我已经完成了读取用户输入的部分。 但是,我不知道如何继续讲述每个数字出现了多少次的部分。

另外,我正在做功课,因此大多数代码都使用芬兰语。 我希望您仍然可以理解它。

using System;

namespace Ohjelma
{
    class Ohjelma
    {
        static void Main()
        {
            Console.Write("Kuinka monta lukua? ");
            int pituus = Convert.ToInt32(Console.ReadLine());

            int[] luvut = new int[pituus];


            for (int i = 0; i < pituus; i++)
            {
                Console.Write("Anna {0}. luku:", i + 1);
                luvut[i] = Convert.ToInt32(Console.ReadLine());
            }

            for (int i = 0; i < luvut.Length; i++)
            {
                Console.Write(luvut[i]);

            }
            Console.ReadLine();
        }
    }
}

编辑:很抱歉,代码块应该输出什么示例,即使我尝试过,也不能完全确定如何使用代码块引用。 谢谢!

int[] num = { 1, 1, 1, 3, 3, 4, 5, 6, 7, 0 };
int[] count = new int[10];

//Loop through 0-9 and count the occurances
for (int x = 0; x < 10; x++){
    for (int y = 0; y < num.Length; y++){
        if (num[y] == x)
            count[x]++;
    }
}

//For displaying output only            
for (int x = 0; x < 10; x++)
    Console.WriteLine("Number " + x + " appears " + count[x] + " times");

程序输出:

Number 0 appears 1 times
Number 1 appears 3 times
Number 2 appears 0 times
Number 3 appears 2 times
Number 4 appears 1 times
Number 5 appears 1 times
Number 6 appears 1 times
Number 7 appears 1 times
Number 8 appears 0 times

我知道当您所有的同学都完成他们的学业后,您仍在苦苦挣扎的感觉。 我的代码应该足够简单以供您学习。

您可以像这样使用LINQ:

var query = luvut.GroupBy(r => r)
                .Select(grp => new
                {
                    Value = grp.Key,
                    Count = grp.Count()
                });

对于输出,您可以使用:

foreach (var item in query)
{
    Console.WriteLine("Value: {0}, Count: {1}", item.Value, item.Count);
}

如果您不想使用LINQ,可以编写以下代码:

public class Program 
{
   public static void Main()
   {
       int[] arr1 = new int[] {1,3,3,5,5,4,1,2,3,4,5,5,5};

       List<int> listArr1 = arr1.ToList();

       Dictionary<int,int> dict1 = new Dictionary<int,int>();

       foreach(int i in listArr1)
       {
          if(dict1.ContainsKey(i))
          {
              int value  = dict1[i];
              value++;
              dict1[i]= value;
          }
          else
          {
              dict1.Add(i,1);
          }
       }

       for(int x = 0 ; x < dict1.Count(); x++)
       {        
          Console.WriteLine("Value {0} is repeated {1} times", dict1.Keys.ElementAt(x),dict1[dict1.Keys.ElementAt(x)]);                       
       }
   }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM