'Write a program that removes from a given sequence all numbers that appear an odd count of times

net from Nakov's Fundamental of computer Programming. Just finished Linear data structures chapter and got stuck with the question. After several attempts I have written this code. Though it gives the desired result. Need help to know how this modified to be more efficient without using hash table.

class RemoveNumberAppearingOddCountTimes
{
    static void Main(string[] args)
    {
        Console.WriteLine("Enter a List of Random integers such that some of them appears Odd times");
        List<int> myList = new List<int>();
        ValidatingList(myList);
        CheckIfOddCount(myList);
        int i = 0;
        do
        {
            Console.WriteLine(myList[i]);
            i++;
        }
        while (i != myList.Count);
        
        Console.ReadLine();
    }
    public static List<int> ValidatingList(List<int> list)
    {
        string input = Console.ReadLine();

        while (input != "")
        {
            try
            {
                list.Add(int.Parse(input));
            }
            catch (FormatException Fe)
            {
                Console.WriteLine(Fe.Message);
            }
            catch (OverflowException OFe)
            {
                Console.WriteLine(OFe.Message);
            }
            input = Console.ReadLine();
        }
        return list;
    }
    public static List<int> CheckIfOddCount(List<int> InputList)
    {
        int AppearCount = 1;
        bool result = false;
        int checkvalue = 0;
        int i = 0;
        for (; i < InputList.Count;i++ )
        {
            checkvalue = InputList[i];
           for (int start = 0; start < InputList.Count;start++)
            {
                if (InputList[i] == InputList[start] && start != i)
                {
                    AppearCount++;
                }
            }
            result = IsOdd(AppearCount);
            if (result == true)
            {
                InputList = OddNumberRemoveFromList(InputList[i], InputList);
                i = 0;
                AppearCount = 1;
            }
            else
            {
                AppearCount = 1;
            }
        }
        return InputList;
    }
    public static List <int> OddNumberRemoveFromList(int number, List<int> RemoveFromList)
    {
        for (int i = 0; i <RemoveFromList.Count; )
        {
            if (RemoveFromList[i] == number)
            {
                RemoveFromList.RemoveAt(i);
                i = 0;
            }
            else i++;
        }
        return RemoveFromList;    
    }
    public static bool IsOdd(int ApearCount)
    {
        if (ApearCount % 2 != 0)
        {
           return true;
        }
        else return false;    
    
    }
}


Solution 1:[1]

You can use Dictionary<int,int> to keep track of how many times each number appears during input already.

Assume You have empty Dictionary<int,int> counts = new(); When You input a number input, instead of putting it to a list, You just increment a count on the dictionary entry, ie

if (counts.ContainsKey(input))
   counts[input]++;
else
   counts[input] = 1;

After that just iterate over Dictionary and print values with odd counts

foreach (var keyValue in counts)
  if (keyValue.Value % 2 == 0)
    Console.WriteLine(keyValue.Key);

Or even better - use a Dictionary<int,bool> to just keep track which entries appear odd number of times

Dictionary<int,bool> isOdd = new();

/// process input

if (!isOdd.Contains(input))
   isOdd[input] = false;
else 
   isOdd[input] = !isOdd[input];

Solution 2:[2]

In the real World one would just do

return InputList.Where(z => InputList.Count(y => y == z) % 2 == 0);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 83lynx
Solution 2 Richard Barraclough