Advertisement
stoianpp

linear 6

Mar 16th, 2014
169
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.  
  5. //Write a program that removes from given sequence all numbers that occur odd number of times.
  6.  
  7. class Task5
  8. {
  9.     static void Main()
  10.     {
  11.         Console.WriteLine("Enter some integers separated by space.For end hit \"return\"");
  12.         LinkedList<int> list = AddingToList(Console.ReadLine());
  13.         list = RemoveOdds(list);
  14.         Console.WriteLine(string.Join(" ", list));
  15.     }
  16.  
  17.     private static LinkedList<int> RemoveOdds(LinkedList<int> list)
  18.     {
  19.         Dictionary<int,int> counter = new Dictionary<int,int>();
  20.         foreach (int item in list)
  21.         {
  22.             if (counter.Keys.Contains(item))
  23.             {
  24.                 counter[item]++;
  25.                 continue;
  26.             }
  27.             counter.Add(item,1);
  28.         }
  29.    
  30.     var node = list.First;
  31.         while (node != null)
  32.         {
  33.             var next = node.Next;
  34.             if (counter[node.Value] % 2 != 0)
  35.             {
  36.                 list.Remove(node);
  37.             }
  38.             node = next;
  39.         }
  40.         return list;
  41.     }
  42.  
  43.     public static LinkedList<int> AddingToList(string input)
  44.     {
  45.         LinkedList<int> result = new LinkedList<int>();
  46.         string[] strList = input.Split(' ');
  47.         foreach (string item in strList) result.AddLast(int.Parse(item));
  48.         return result;
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement