C#,如何在另一个二维数组中保存一个数组中元素的频率?

所以这是我有问题的代码。当我尝试传递带有N个参数的数组时,假设结果为{2,1,2,2,5},我想得到二维的secArray [element,元素的频率]。问题是我得到的不止于此,在这种情况下,我得到的数组如下:23 11 22 21 52


        Console.WriteLine("Enter number of elements: ");

        int n = int.Parse(Console.ReadLine());

        int[] array = new int[n];

        for (int i = 0; i < array.Length; i++)

        {

            Console.Write("Array[{0}]: ", i);

            array[i] = int.Parse(Console.ReadLine());

        }


        //problematic code begins

        int[,] secArray = new int[n,2];


        for(int i = 0;i<n;i++)

        {

            for(int j = 0; j<n;j++)

            {


                if(array[i] == secArray[j,0])

                {

                    secArray[j, 1] += 1;

                }


                else

                {

                    secArray[i, 0] = array[i];

                    secArray[i, 1] = 1;

                }

            }

        }

        //problematic code ends

        //printing - works good

        Console.WriteLine("How many same elements?");

        for (int row = 0; row < secArray.GetLength(0); row++)

        {

            for (int col = 0; col < secArray.GetLength(1); col++)

            {

                Console.Write(secArray[row, col]);

            }

            Console.WriteLine();

        }

如果有人知道如何解决此问题,我将不胜感激。我不知道实际问题在哪里,这让我感到沮丧。


翻翻过去那场雪
浏览 161回答 3
3回答

暮色呼如

第一个问题涉及第一个陈述。int[,] secArray = new int[n,2];在遍历数组之前,您不知道数组中有多少个唯一元素。您不能使用n,因为n是参数的总数,可以大于唯一元素的数量。其次,嵌套的for循环效率很低。您的算法会遍历数组中每个元素的数组,因此它将在O(n ^ 2)时间内运行。想想:您是否必须多次遍历数组?为什么不使用哈希表(C#中的字典)在遍历数组时跟踪计数?哈希表使用一种非常有效的查找机制来告诉您是否已经看到该元素,并且该值可用于跟踪计数。考虑用以下代码替换有问题的代码,并了解其工作方式。&nbsp; &nbsp; &nbsp; &nbsp; Dictionary<int, int> elementCounts = new Dictionary<int, int>();&nbsp; &nbsp; &nbsp; &nbsp; for(int i = 0; i < n; i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int element = array[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (elementCounts.ContainsKey(element))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elementCounts[element]++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elementCounts.Add(element, 1);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("How many same elements?");&nbsp; &nbsp; &nbsp; &nbsp; foreach(KeyValuePair<int,int> count in elementCounts)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Element: {0} Count: {1}", count.Key, count.Value);&nbsp; &nbsp; &nbsp; &nbsp; }然后,如果要将哈希表(Dictionary)中的结果复制到二维数组,则可以执行以下操作。&nbsp; &nbsp; &nbsp; &nbsp; int numberOfUniqueElements = elementCounts.Count;&nbsp; &nbsp; &nbsp; &nbsp; int[,] secArray = new int[numberOfUniqueElements, 2];&nbsp; &nbsp; &nbsp; &nbsp; int j = 0;&nbsp; &nbsp; &nbsp; &nbsp; foreach (KeyValuePair<int, int> count in elementCounts)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; secArray[j, 0] = count.Key;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; secArray[j, 1] = count.Value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j++;&nbsp; &nbsp; &nbsp; &nbsp; }

慕标5832272

我会用Linq的GroupBy来做到这一点var&nbsp;array&nbsp;=&nbsp;new&nbsp;int[]&nbsp;{&nbsp;2,&nbsp;1,&nbsp;2,&nbsp;2,&nbsp;5&nbsp;}; var&nbsp;result&nbsp;=&nbsp;array.GroupBy(x&nbsp;=>&nbsp;x).Select(x&nbsp;=>&nbsp;new[]&nbsp;{&nbsp;x.Key,&nbsp;x.Count()&nbsp;}).ToArray();

四季花海

为什么不使用哈希表。令数组中的数字为哈希条目键,令哈希条目的值为计数。然后只需遍历数组一次。在遍历数组时,检查是否存在哈希条目(如果存在),如果没有,则添加1。就像是for(int i = 0; i<n;i++) {&nbsp; if(hashTable.containsKey(array[i])) {&nbsp; &nbsp; hashTable[array[i]]++];&nbsp; } else {&nbsp; &nbsp; hashTable.add(array[i],1);&nbsp; }}请注意,这是quedocode,将需要查找方法并正确实现。
打开App,查看更多内容
随时随地看视频慕课网APP