简体   繁体   English

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

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

So this is what I want my output to look like: 所以这就是我希望输出看起来像的样子:

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

The thing is, I've got the part which reads the user input done. 关键是,我已经完成了读取用户输入的部分。 However, I have no idea how to continue with the part which tells how many times each number appeared. 但是,我不知道如何继续讲述每个数字出现了多少次的部分。

Also, I'm doing this as a schoolwork so most of the code is in Finnish. 另外,我正在做功课,因此大多数代码都使用芬兰语。 I hope you can still understand it, though. 我希望您仍然可以理解它。

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

Edit: Sorry about the code block on the example of what it should output, not exactly sure how to use blockquotes even though I tried. 编辑:很抱歉,代码块应该输出什么示例,即使我尝试过,也不能完全确定如何使用代码块引用。 Thanks! 谢谢!

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

Program Output: 程序输出:

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

I understand how bad it feels when all your classmates had finish theirs, and you are still struggling. 我知道当您所有的同学都完成他们的学业后,您仍在苦苦挣扎的感觉。 My codes should be simple enough for your learning. 我的代码应该足够简单以供您学习。

You can use LINQ like: 您可以像这样使用LINQ:

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

For output you can use: 对于输出,您可以使用:

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

If you don't want to use LINQ, you can code as follows:- 如果您不想使用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.

相关问题 解析一个字符串数组以检查一个值有多少次 - Parse through an array of strings to check how many times there's a value C#-一个特定的int值在数组中出现多少次? 没有Linq技术 - C# - How many times a specific int value appears in array? No Linq tech 如何显示数组元素出现的次数 - How to display how many times an array element appears 如何检查一个数字在文本文件中的特定行中出现了多少次 - How to check how many times a number appears in a specific line inside a text file 计算字段属性出现在列中的次数 - Count how many times a field attribute appears in a column 计算在出现任何其他字符之前某个字符串出现在字符串中的次数 - Counting how many times a certain char appears in a string before any other char appears 计算一个值在数组中出现的次数 - Counting the number of times a value appears in an array 如何统计一个字符串在二维数组的某一列中出现的次数? - How to count the number of times a string appears in a column in a 2D array? 如何检查一个字符串在另一个字符串中存在多少次 - How to check how many times a string exist in another string 如何查找同一对象在列表中出现的次数,然后找到最常出现的对象的属性 - How to find how many times the same object appears in a list and then find a property of the most appeared object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM