svetlozar_kirkov

Number of Occurences (Exercise)

Oct 3rd, 2014
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.13 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5.  
  6. namespace ConsoleTesting3
  7. {
  8.     class ConsoleTesting3
  9.     {
  10.         private static void Main()
  11.         {
  12.             int[] test = new int[10];
  13.             for (int i = 0; i < 10; i++)
  14.             {
  15.                 Console.Write("Index {0}: ", i);
  16.                 test[i] = int.Parse(Console.ReadLine());
  17.             }
  18.             Console.WriteLine();
  19.             NumberOfOccurences(test);
  20.             Console.ReadKey();
  21.  
  22.         }
  23.  
  24.         static void NumberOfOccurences(params int[] array)
  25.         {
  26.             var distincts = array.Distinct().ToArray();
  27.             int count = 0;
  28.             for (int i = 0; i < distincts.Length; i++)
  29.             {
  30.                 for (int j = 0; j < array.Length; j++)
  31.                 {
  32.                     if (array[j] == distincts[i])
  33.                     {
  34.                         count++;
  35.                     }
  36.                 }
  37.                 Console.WriteLine("Number \"{0}\" found {1} times.", distincts[i], count);
  38.                 count = 0;
  39.             }
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment