Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- //Write a program that removes from given sequence all numbers that occur odd number of times.
- class Task5
- {
- static void Main()
- {
- Console.WriteLine("Enter some integers separated by space.For end hit \"return\"");
- LinkedList<int> list = AddingToList(Console.ReadLine());
- list = RemoveOdds(list);
- Console.WriteLine(string.Join(" ", list));
- }
- private static LinkedList<int> RemoveOdds(LinkedList<int> list)
- {
- Dictionary<int,int> counter = new Dictionary<int,int>();
- foreach (int item in list)
- {
- if (counter.Keys.Contains(item))
- {
- counter[item]++;
- continue;
- }
- counter.Add(item,1);
- }
- var node = list.First;
- while (node != null)
- {
- var next = node.Next;
- if (counter[node.Value] % 2 != 0)
- {
- list.Remove(node);
- }
- node = next;
- }
- return list;
- }
- public static LinkedList<int> AddingToList(string input)
- {
- LinkedList<int> result = new LinkedList<int>();
- string[] strList = input.Split(' ');
- foreach (string item in strList) result.AddLast(int.Parse(item));
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement