1

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!

Tatu
  • 153
  • 1
  • 1
  • 9
  • Perhaps it would help if you wrote out the steps in English (or Finnnish - I just mean "not code") and then see which of those steps you're having trouble writing the code for. – Pete Baughman Apr 10 '14 at 17:52
  • 1
    Possible duplicate of [A method to count occurrences in a list](https://stackoverflow.com/questions/1139181/a-method-to-count-occurrences-in-a-list) – Liam Nov 03 '17 at 08:54

3 Answers3

7

You can use LINQ like:

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);
}
Habib
  • 219,104
  • 29
  • 407
  • 436
6
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.

user3437460
  • 17,253
  • 15
  • 58
  • 106
0

If you don't want to use LINQ, you can code as follows:-

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)]);                       
       }
   }
}
chaosifier
  • 2,666
  • 25
  • 39
Varun Tripathi
  • 141
  • 1
  • 4