-1

Given a list of n values in a list. I've written the below to iterate through the collection and compare each value against the other members.

I feel it is a bit 'hacky' though and there's probably a better way to do it.

Can anyone give any tips?

Console.WriteLine("Enter a series of numbers seperated by a hyphen (-): ");
            var userInput = Console.ReadLine().Split('-');

            var inputList = new List<int>();

            if (userInput.Count() > 1)
            {
                foreach (var item in userInput)
                {
                    inputList.Add(int.Parse(item));
                }
            }
            else
            {
                Console.WriteLine("Exiting....");
                return;
            }

            var count = 0;

            foreach (var num in inputList)
            {
                for (int i = 0; i < inputList.Count; i++)
                {
                    if (num == inputList[i])
                    {
                        count++;
                    }

                    if (count != inputList.Count())
                    {
                        break;
                    }
                }
            }
Keva161
  • 2,623
  • 9
  • 44
  • 68
  • 3
    `list.Count == list.Distinct().Count()` – Mr Anderson Jul 23 '19 at 18:25
  • 1
    `var inputList = userInput.Select(int.Parse).ToList();` – Rufus L Jul 23 '19 at 18:27
  • 1
    If you want a list of the duplicated values: `var duplicates = list.GroupBy(n => n).Where(g => g.Skip(1).Any()).Select(g => g.Key).ToList();` – 15ee8f99-57ff-4f92-890c-b56153 Jul 23 '19 at 18:27
  • 2
    Your code won't actually work - your test for `count != inputList.Count()` (ps don't use `inputList.Count` and `inputList.Count()` in the same code) will break out of the inner loop early every time. – NetMage Jul 23 '19 at 18:28
  • 1
    Without using LINQ, the canonical way to test for duplicates in an `IEnumerable` is to add each member to a `HashSet` and note when the `Add` returns `false` indicating a duplicate add attempt. – NetMage Jul 23 '19 at 18:30
  • 1
    Also, `Split` returns `string[]`, use `Length` on an array, not `Count()`. – NetMage Jul 23 '19 at 18:31
  • If you're allowed to use LINQ, then use `GroupBy` as suggested above. If not allowed, then your code is fine except for `if (count != inputList.Count())`, not sure what you're trying to do with that that code. It will work without it. By "fine" I don't mean that's the only way but it is definitely not am odd way so it should be fine. If you care whether it has duplicates, a yes or no, then as soon as you find one, break. If you need the count then keep what you have. – CodingYoshi Jul 23 '19 at 18:38

1 Answers1

1

Here is the non-LINQ way of tracking duplicates using a HashSet:

Console.WriteLine("Enter a series of numbers separated by a hyphen (-): ");
var inputList = Console.ReadLine().Split('-').Select(s => Int32.Parse(s)).ToList();

if (inputList.Count <= 1) {
    Console.WriteLine("Exiting....");
    return;
}

var nonDups = new HashSet<int>();
var dupCount = 0;
foreach (var num in inputList) {
    if (!nonDups.Add(num)) {
        ++dupCount;
    }
}

Console.WriteLine($"There {(dupCount == 1 ? "was" : "were")} {dupCount} duplicate{(dupCount == 1 ? "" : "s")}");
NetMage
  • 26,163
  • 3
  • 34
  • 55
  • You wrote a condition to make the message grammatically correct? Plus one for that alone. – CodingYoshi Jul 23 '19 at 18:56
  • I am one of those people who cannot stand code comments which are not grammatically correct let alone messages the public can see. – CodingYoshi Jul 23 '19 at 18:57
  • @CodingYoshi One of my standard extension methods: `public static string ToPluralSuffix(this int n, string suffix = "s") => n == 1 ? "" : suffix;` – NetMage Jul 23 '19 at 19:36