TodorMitev

ExtractOddOccuredStrings

Mar 1st, 2012
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.40 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace _02ExtractOddStrings
  7. {
  8.     class OddStrings
  9.     {
  10.  
  11.         static void ExtractOddOccuredStrings(string[] str, Dictionary<string, int> dictionary)
  12.         {
  13.             foreach (var s in str)
  14.             {
  15.                 int occurs = 1;
  16.                 if (dictionary.ContainsKey(s))
  17.                 {
  18.                     occurs = dictionary[s] + 1;
  19.                 }
  20.                 dictionary[s] = occurs;
  21.             }
  22.         }
  23.         static void Print(Dictionary<string, int> dict)
  24.         {
  25.             int size = dict.Count;
  26.             int count = 0;
  27.             Console.Write("{");
  28.             foreach (var item in dict)
  29.             {
  30.                 if (item.Value %2==1)
  31.                 {
  32.                     Console.Write("{0}", item.Key, item.Value);
  33.                 }
  34.                 if (count < size-2)
  35.                 {
  36.                     Console.Write(", ");
  37.                 }
  38.                 count++;
  39.             }
  40.             Console.WriteLine("}");
  41.         }
  42.         static void Main(string[] args)
  43.         {
  44.             Dictionary<string, int> dictionary = new Dictionary<string, int>();
  45.             string[] str = { "C#", "SQL", "PHP", "PHP", "SQL", "SQL" };
  46.  
  47.             ExtractOddOccuredStrings(str, dictionary);
  48.             Print(dictionary);
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment