Advertisement
georgimanov

12. Count Of Names

Apr 13th, 2014
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.31 KB | None | 0 0
  1. /*
  2. Problem 12. Count of Names
  3. Write a program that reads a list of names and prints for each name how many times it appears in the list. The names should be listed in alphabetical order. Use the input and output format from the examples below. Examples:
  4. Input  
  5.  * Output
  6.  *
  7. Peter Steve Nakov Steve Alex Nakov 
  8.  * Alex -> 1
  9.  * Nakov -> 2
  10.  * Peter -> 1
  11.  * Steve -> 2
  12.  *
  13. Nakov Nakov Nakov SoftUni Nakov S
  14.  * SoftUni -> 1
  15.  * Nakov -> 5
  16. */
  17.  
  18.  
  19. using System;
  20. using System.Collections.Generic;
  21. using System.Linq;
  22.  
  23. class CountLetter
  24. {
  25.     static void Main(string[] args)
  26.     {
  27.         string input = Console.ReadLine();
  28.  
  29.         string[] arr = input.Split(' ');
  30.  
  31.         Array.Sort(arr); //sorting also can be done with 2 for loops
  32.  
  33.         List<string> printedNames = new List<string>();
  34.  
  35.         int counter = 0;
  36.         for (int i = 0; i < arr.Length; i++)
  37.         {
  38.             for (int j = 0; j < arr.Length; j++)
  39.             {
  40.                 if (arr[i] == arr[j])
  41.                 {
  42.                     counter++;
  43.                 }
  44.             }
  45.             if (!printedNames.Contains(arr[i]))
  46.             {
  47.                 Console.WriteLine("{0} -> {1}", arr[i], counter);
  48.                 counter = 0;
  49.                 printedNames.Add(arr[i]);
  50.             }
  51.            
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement