JulianJulianov

02.AssociativeArray - Odd Occurrences

Apr 12th, 2020
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.34 KB | None | 0 0
  1. 02.Odd Occurrences
  2.  
  3. Write a program that extracts all elements from a given sequence of words that are present in it an odd number of times (case-insensitive).
  4. • Words are given on a single line, space separated.
  5. • Print the result elements in lowercase, in their order of appearance.
  6. Examples
  7. Input                                      Output
  8. Java C# PHP PHP JAVA C java                java c# c
  9. 3 5 5 hi pi HO Hi 5 ho 3 hi pi             5 hi
  10. a a A SQL xx a xx a A a XX c               a sql xx c
  11.  
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Linq;
  15.  
  16. namespace _02OddOccurrences
  17. {
  18.     class Program
  19.     {
  20.         static void Main(string[] args)
  21.         {
  22.             var words = Console.ReadLine().ToLower().Split().ToArray();
  23.             var counts = new Dictionary<string, int>();
  24.  
  25.             foreach (var item in words)
  26.             {
  27.                 if (counts.ContainsKey(item))
  28.                 {
  29.                     counts[item]++;
  30.                 }
  31.                 else
  32.                 {
  33.                     counts.Add(item, 1);
  34.                 }
  35.             }
  36.             foreach (var item in counts)
  37.             {
  38.                 if (item.Value % 2 != 0)
  39.                 {
  40.                     Console.Write(item.Key + " ");
  41.                 }
  42.             }
  43.             Console.WriteLine();
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment