Advertisement
TodorMitev

OccursInArray

Mar 1st, 2012
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace DictionariesHashSets
  7. {
  8.    
  9.     class Occurs
  10.     {
  11.         //static int[] array = { 3, 4, 4, 3, 3, 4, 3, 2 };
  12.         static void Print(SortedDictionary<int,int> dict)
  13.         {
  14.             foreach (var item in dict)
  15.             {
  16.                 if (item.Value > 1)
  17.                 {
  18.                     Console.WriteLine("{0} -> {1} times", item.Key, item.Value);
  19.                 }
  20.                 else
  21.                 {
  22.                     Console.WriteLine("{0} -> {1} time", item.Key, item.Value);
  23.                 }
  24.             }
  25.         }
  26.         static void Main(string[] args)
  27.         {
  28.             Console.WriteLine("Number of integers to input?:");
  29.             int n = int.Parse(Console.ReadLine());
  30.             int[] array = new int[n];
  31.             for (int i = 0; i < array.Length; i++)
  32.             {
  33.                 array[i] = int.Parse(Console.ReadLine());  
  34.             }
  35.  
  36.             SortedDictionary<int, int> dict = new SortedDictionary<int, int>();
  37.             foreach (int item in array)
  38.             {
  39.                 int occurs = 1;
  40.                 if (dict.ContainsKey(item))
  41.                 {
  42.                     occurs = dict[item] + 1;
  43.                 }
  44.                 dict[item] = occurs;
  45.             }
  46.             Print(dict);
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement