数字出现在数组中的次数列表

时间:2017-04-21 19:12:23

标签: c# arrays

我必须制作 n 元素的数组,并找出每个数字出现的次数,如下所示:

Array: (-1.7 ; 3.0 ; 0.0 ; 1.5 ; 0.0 ; -1.7 ; 2.3 ; -1,7)
-1.7 appears 3 times
 3.0 appears 1 time
 0.0 appears 2 times
 1.5 appears 1 time
 2.3 appears 1 time

我试过了,我的代码看起来像这样:

        int n = 0;
        Console.WriteLine("Type in the size of your array...");
        n = int.Parse(Console.ReadLine());

        float[] vet = new float[n];
        int[] freq = new int[n];

        Console.WriteLine("Now, type in each element...");
        for(int i = 0; i < vet.Length; i++)
        {
            Console.Write("Position {0}: ", i);
            vet[i] = float.Parse(Console.ReadLine());
        }
        for(int j = 0; j< vet.Length; j++)
        {
            for(int k = 0; k< freq.Length; k++)
            {
                if(vet[j] == vet[k])
                {
                    freq[j]++;
                }
            }
        }
        Console.WriteLine("The number of times each element appears in the array is:");

        for(int l = 0; l< freq.Length; l++)
        {
            Console.WriteLine("{0} appears {1} time(s)", vet[l], freq[l]);
        }
        Console.ReadKey();

但输出仍然如下:

The number of times each element appears in the array is:
-1.7 appears 3 time(s)
 3.0 appears 1 time(s)
 0.0 appears 2 time(s)
 1.5 appears 1 time(s)
 0.0 appears 2 time(s)
-1.7 appears 3 time(s)
 2.3 appears 1 time(s)
-1.7 appears 3 time(s)

我的问题是:如何让我的代码以重复数字打印一次的方式工作,就像第一个例子一样?

4 个答案:

答案 0 :(得分:3)

我个人会使用linq,如:

questions.controls

DotNetFiddle.Net

结果:

  

-1.7出现2次

     

3.0出现1次

     

0.0出现2次

     

1.5出现1次

     

2.3出现1次

     

-1出现1次

     

7出现1次

答案 1 :(得分:2)

尝试使用Dictionary代替。这是代码:

    var arr = new double [] { -1.7, 3.0, 0.0, 1.5, 0.0, -1.7, 2.3, -1.7 };
    var dic = new Dictionary<double, int>();

    foreach (var element in arr)
    {
        if (dic.ContainsKey(element))
            dic[element]++;
        else
            dic[element] = 1;
    }

    foreach (var element in dic)
    {
        Console.WriteLine(element.Key + " appears " + element.Value + " time(s)");
    }

答案 2 :(得分:1)

Nononononono。您没有使用C#的强大功能。这可能也是一个c ++程序。更好的方法是使用类似

之类的方法将数组转换为列表
List<float> list=vet.ToList()

然后您可以按照此处的信息:Get a list of distinct items and their count

我会在这里写一下:

var query = list.SelectMany(x => x.Names)
            .GroupBy(s => s)
            .Select(g => new { Name = g.Key, Count = g.Count() });

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

答案 3 :(得分:1)

典型的解决方案基于 Linq

   $('#mything').on('click', function() {
      $('#stuff2').hide(); 

   })

结果:

using System.Linq;

...

var freqs = vet
  .GroupBy(item => item)
  .OrderBy(chunk => chunk.Key)
  .Select(chunk => 
     $"{chunk.Key,4:f1} appears {chunk.Count()} {(chunk.Count() > 1 ? "times" : "time")}");

Console.WriteLine("The number of times each element appears in the array is:");

Console.Write(string.Join(Environment.NewLine, freqs));

Console.ReadKey();