How to count occurences of an int in an array?

后端 未结 5 984
执念已碎
执念已碎 2021-01-07 15:57

My array is A = {2, 3, 4, 3, 4, 2, 4, 2, 4}

I need an array B that stock at the index i the number of occurences of i in the a

5条回答
  •  一整个雨季
    2021-01-07 16:20

    If you are looking for number of occurences, I have made some examples, still I'm not really sure what do you mean by saying if you add arrays in A they should appear in B. To have this kind of funcionality you have to use some notification framework and cannot have simple arrays. At the very least you should wrap all functions where you want to add an element to A and make calculation like i have showed in third example (with result array E).

                int[] a = new int[] { 2, 3, 4, 3, 4, 2, 4, 2, 4 };
    
            //version 1 - unsorted array
            //find top number of A array
            int max_number_a = a.Max() + 1;
            //initialize B,C of that size
            int[] b = new int[max_number_a];    //RESULT linq version
            int[] c = new int[max_number_a];    //RESULT double loop version
            for (int i = 0; i < max_number_a; i++)
            {
                //this is linq way
                b[i] = a.Where(x => x == i).Count();
    
                //this is double loop way
                c[i] = 0;           //initialize position so we can later count/increment when we find each instance of i inside A array
                for (int j = 0; j < a.Length; j++)
                {
                    if (a[j] == i)   //check if a[j] is the number we are searching for
                        c[i]++;      //we have found one instance of J number, increase the B[i]
                }
            }
    
            //version 2 - sorted array
            int[] d = new int[max_number_a];    //RESULT sorted array
            //initialize all to zero
            for (int i = 0; i < max_number_a; i++) d[i] = 0;        //initialize array to zeros so we can count
            List aList = a.OrderBy(x => x).ToList();            //this is linq ordering, choose any other way to order it
            while (aList.Count > 0)                                 // we have to use all a elements
            {
                d[aList[0]]++;
                aList.RemoveAt(0);
            }            
    
            //version 3 - the simple (right) way, and probably what you should be doing :)
            int[] e = new int[max_number_a];
            //initialize all to zero
            for (int i = 0; i < max_number_a; i++) e[i] = 0;        //initialize array to zeros so we can count
            for (int i = 0; i < a.Length; i++)
            {
                //we take e index of a[i] and increments its value
                e[a[i]]++;                             
                /*
                 * int number_in_a = a[i];
                 * int e_index_value = e[number_in_a];
                 * e[number_in_a] = e_index_value + 1;
                 */
            }
    

提交回复
热议问题