1
Reply

Write a method which eliminates pair numbers from continuous stream of numbers read one at a time until no more numbers are returned. Numbers are not read in order and there can be duplicates. For example : {3,7,9,41,21, 56, 8, 2} should become {9,41,21,56}, so pairs like 2,3 and 7,8 needs to be eliminated. out of 10

s tripathi

s tripathi

7y
123
0
Reply

    class Program{private static SortedSet ArrayInt;static void Main(string[] args){var lclNumbers = new Numbers();ArrayInt = new SortedSet();while (!lclNumbers.Done()){var number = lclNumbers.GetNextNumber();Console.WriteLine(number);ArrayInt.Add(number);AdjustPairs(ArrayInt);}PrintElements(ArrayInt);Console.ReadKey();}private static void PrintElements(SortedSet ArrayInt) {foreach (var item in ArrayInt) Console.WriteLine(item);Console.WriteLine("=================");}private static void AdjustPairs(SortedSet ArrayInt){for (int i = 0; i < ArrayInt.Count - 1; i++){if (i == ArrayInt.Count-1) break;var current = ArrayInt.ElementAt(i);var next = ArrayInt.ElementAt(i + 1);PrintElements(ArrayInt);if (current == next - 1){ArrayInt.Remove(current);ArrayInt.Remove(next);}}PrintElements(ArrayInt);}}class Numbers{static int icount=0;public bool Done() {if (icount > 100) { return true; } else { return false; }}public int GetNextNumber(){Random rndnumber = new Random();icount++;return rndnumber.Next(1, 100);}}I have used SortedSet and Random numbers upto 1000. There is no upper limit how many incoming numbers there may be. For testing purposes I have used max 1000. At some point this may cause exception if input numbers max is very high.