0
        List<int> numberList = new List<int>();
        List<int> uniqueList = new List<int>();

        string input;
        int number = 0;
        bool over = false;

        while (!over)
        {
            input = Console.ReadLine();
            if (input == "quit")
            {
                over = true;
                break;
            }
            if (int.TryParse(input, out number))
            {
                numberList.Add(number);
            }
        }

        for (int i = 0; i < numberList.Count; i++)
        {
            if (!uniqueList.Contains(number))
            {
                uniqueList.Add(number);
            }
        }

        for (int i = 0; i < uniqueList.Count; i++)
        {
            Console.WriteLine(uniqueList[i]);
        }

        Console.ReadKey();

Hi! I need some help with this program that takes in numbers, but upon typing "quit", it should list all the unique numbers that have been entered.

For example here's the input: 5, 5, 5, 2, 2, 1, 3 The console should only display 1 and 3 because they are the only unique numbers.

The problem is, that the program in its current form only displays the last unique number that has been entered, not all of them and I don't know why.

Could you tell me why? What do I need to do to list all of them?

lapartman
  • 45
  • 1
  • 7
  • When you debugged through it, what did the code do that you expected it not to do? – mjwills Aug 28 '18 at 10:09
  • 1
    Your code simply ignores duplicates, it does not keep the numbers referenced only one. To do that: `numberList.GroupBy(number => number).Where(group => (group.Count() == 1)).Select(group => group.Key)`. Also, using `Contains` with a list is very inefficient. You could use a `Dictionary` to store the numbers along with their number of occurences. – vc 74 Aug 28 '18 at 10:10

4 Answers4

3

You need to use a Dictionary to record the count of each number, then return the unique ones:

    Dictionary<int, int> numberDictionary = new Dictionary<int, int>();

    string input;
    int number = 0;
    bool over = false;

    while (!over)
    {
        input = Console.ReadLine();
        if (input == "quit")
        {
            over = true;
            break;
        }
        if (int.TryParse(input, out number))
        {
            if (numberDictionary.TryGetValue(number), out int count)
            {
                numberDictionary[number] = count + 1;
            }
            else
            {
                numberDictionary.Add(number, 1);
            } 
        }
    }

    foreach(var item in numberDictionary)
    {
        if (item.Value == 1)
        Console.WriteLine(item.Key);
    }

    Console.ReadKey();
mjwills
  • 23,389
  • 6
  • 40
  • 63
ste-fu
  • 6,879
  • 3
  • 27
  • 46
  • 1
    Remove the numberDictionary double lookup and you'll get my vote. – vc 74 Aug 28 '18 at 10:38
  • Thank you, this is my favorite answer. The answer above yours is working too, I just don't know LINQ yet, but I used Dictionary once in a Unity course. So this is more understandable for me. – lapartman Aug 28 '18 at 10:39
  • 1
    Now you can have it ;) – vc 74 Aug 29 '18 at 12:23
1

You can do this much simpler

List<int> numbers = new List<int>{1, 2, 2, 3, 3, 3, 4, 4, 4, 5, 6, 6, 6};
var unique = numbers.Where(n => numbers.Count(m => m == n) == 1);
foreach(var u in unique)
    WriteLine(u);

Prints

1

5

What this is doing is it is takign the List numbers, and it Where looks at each member in the list (in this case that member is called n).

It then checks if the number of occurences of n is equal to 1. If it is this must be unique.

So for the first number in our list 1, the following:

numbers.Count(m => m == n)

Will return 1.

When n is 2 then

numbers.Count(m => m == n)

Will return 2.

Community
  • 1
  • 1
Stuart
  • 3,949
  • 7
  • 29
  • 58
1

This is a full implementation of the solution :

using System;
using System.Collections.Generic;
using System.Linq;

namespace EntryPoint.Concole
{
    class Program
    {
        static void Main(string[] args)
        {
           List<int> numberList = new List<int>();
           List<int> uniqueList = new List<int>();

           string input;
           int number = 0;
           bool over = false;

           while (!over)
           {
               input = Console.ReadLine();
               if (input == "quit")
               {
                  over = true;
                  break;
               }
               if (int.TryParse(input, out number))
               {
                  numberList.Add(number);
               }
           }

           var numbersDistinct = numberList.GroupBy(i => i);

           foreach (var num in numbersDistinct)
           {
              if (num.Count() == 1)
              {
                  uniqueList.Add(num.Key);
              }
           }

           for (int i = 0; i < uniqueList.Count; i++)
           {
              Console.WriteLine(uniqueList[i]);
           }

           Console.ReadKey();
        }
    }
Smaiil
  • 139
  • 1
  • 5
0

I have checked and fixed your code. Although it is not perfect but I have pointed out the crucial mistakes.

        List<int> numberList = new List<int>();
        List<int> uniqueList = new List<int>();

        string input;
        int number = 0;
        bool over = false;

        while (!over)
        {
            input = Console.ReadLine();
            if (input == "quit")
            {
                over = true;
                //break; // it is not needed since you have over = true
            }
            if (int.TryParse(input, out number))
            {
                numberList.Add(number);
            }
        }

        for (int i = 0; i < numberList.Count; i++)
        {
            //if (!uniqueList.Contains(number)) // this is the wrong line
            if (numberList.Count(q => q == numberList[i]) == 1)
            {
                //uniqueList.Add(number); // this is also wrong
                uniqueList.Add(numberList[i]);
            }
        }

        for (int i = 0; i < uniqueList.Count; i++)
        {
            Console.WriteLine(uniqueList[i]);
        }

        Console.ReadKey();

In simple words the Count(...) method in the

numberList.Count(q => q == numberList[i]) == 1

line just counts elements of the numberList which satisfy provided condition. In this case, condition is q == numberList[i] which basically means to count elements that equal to numberList[i].

You may also read about delegates and lambda expressions in C# for better understanding.

Unknown User
  • 78
  • 1
  • 7
  • Thank you, I forgot to remove "break". Could explain the LINQ line? I haven't used them before and I'd like to understand it. – lapartman Aug 28 '18 at 10:44