Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Count occurrences of number in array C#

I have an array moviesRented with the following values stored in it:
17 3 3 2 2 2 1 1 1 1. The values were read from parsed data in a txt file

I need to print out the count of each value. For example the output should be:
4 occurrence of 1
3 occurrence of 2
2 occurrence of 3
1 occurrence of 17

Here is my current code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

            StreamReader inFile = new StreamReader("project-files/transactions.txt");
            string line = inFile.ReadLine();

            while(line != null){

                string[] temp = line.Split("#");
            
                moviesRented[count] = int.Parse(temp[2]);                
                count++;

                line = inFile.ReadLine();
            }

            inFile.Close();

            for(int i = 0; i < count - 1; i++){
                for(int j = i + 1; j < count; j++){

                    if(moviesRented[i] < moviesRented[j]){
                        int temp = moviesRented[i];
                        moviesRented[i] = moviesRented[j];
                        moviesRented[j] = temp;
                    }
                }
            }

            // output sorted array
            for(int i = 0; i < count; i++){
                Console.WriteLine(moviesRented[i]);
            }

I’m able to get the needed values from the text file (they are stored in the moviesRented array) and I also sorted them from highest to lowest.

Here is the moviesRented output after the sort:
17
3
3
2
2
2
1
1
1
1

How would I go about counting the values? I tried something like this:

        StreamReader inFile = new StreamReader("project-files/transactions.txt");
        string line = inFile.ReadLine();

        while(line != null){

            string[] temp = line.Split("#");
        
            moviesRented[count] = int.Parse(temp[2]);                
            count++;

            line = inFile.ReadLine();
        }

        inFile.Close();

        for(int i = 0; i < count - 1; i++){

            int tempCount = 0;
            for(int j = i + 1; j < count; j++){

                if(moviesRented[i] < moviesRented[j]){
                    int temp = moviesRented[i];
                    moviesRented[i] = moviesRented[j];
                    moviesRented[j] = temp;
                }

                // this is new compared to the code above
                // it compares iteration with next iteration
                // and if they match then add 1 to tempCount
                if(moviesRented[i] == moviesRented[j]){
                    tempCount++;
                }
            }
            Console.WriteLine($"{moviesRented[i]} {tempCount}");
        }

Which counts it, but there are too many instances of each value – here is the output with the code above:
17 1
3 1
3 2
2 2
2 1
2 3
1 3
1 2
1 1

But I need the HIGHEST occurrence value of each word – not all of them like in the output above.

How would I go about displaying (printing to console) each moviesRented value and how many times it occurs in descending order such as the first output I mentioned above:

4 occurrence of 1
3 occurrence of 2
2 occurrence of 3
1 occurrence of 17

>Solution :

The easiest way is to use LINQ:

var statistics = from x in moviesRented
                 group x by x into g
                 select new { Number = g.Key, Count = g.Count() };

foreach(var statItem in statistics)
   Console.WriteLine($"{statItem.Count} occurence(s) of {statItem.Number}");

The well-known classic algorithm is to use dictionary (hashtable), which contains incremented count of each value.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading