Advertisement
VladoG

[Lists]-Exercise - 07-Count Numbers-1

Apr 10th, 2016
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.44 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _07_Count_Numbers
  8.     {
  9.     class CountNumbers
  10.         {
  11.         static void Main(string[] args)
  12.             {
  13.             string inputStr = "8 2 2 8 2 2 3 7"; // For EXAMPLE only
  14.             //string inputStr = Console.ReadLine();
  15.             List<int> inputL = inputStr.Split().Select(int.Parse).ToList();
  16.             List<int> tempL = inputL;
  17.             int[] countNum = new int[inputL.Max()+1];
  18.             int lenTemp;
  19.  
  20.             // Console.WriteLine("Input List --> "+string.Join(" ", inputL));
  21.  
  22.             foreach (var num in inputL)
  23.                 {
  24.                 lenTemp = tempL.Count;
  25.                 for (int j = 0; j < lenTemp; j++)
  26.                     {
  27.                     if (tempL[j] == num)
  28.                         {
  29.                         countNum[num]++;
  30.                         tempL.RemoveAt(j);
  31.                         lenTemp--;
  32.                         j--;
  33.                         }
  34.                     }
  35.                 } // End of Foreach
  36.  
  37.  
  38.             for (int i = 0; i <= 1000; i++)
  39.                 {
  40.                 if (countNum[i]>0)
  41.                     {
  42.                     Console.WriteLine($"{i} -> {countNum[i]}");
  43.                     }
  44.                 }
  45.  
  46.  
  47.             // Console.WriteLine("\n"+string.Join(" ",countNum));
  48.  
  49.             }
  50.         }
  51.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement