0

I have come across a little problem while I'm trying to learn arrays. So the idea is that I need to print out the unique elements in an array. When I run the program, its printing out more than the number of values in the array lol. I'm sorry if this is a super simple and dumb question but i'm not seeing the problem with it. Language is c#.

static void Main(string[] args)
    {
        int[] numbers = new int[10];
        Random random = new Random();

        for (int i = 0; i < 10; i++)
        {
            numbers[i] = random.Next(0, 10);
        }

        Array.Sort(numbers);
        foreach (var num in numbers)
        {
            Console.Write("{0}, ", num);
        }

        Console.WriteLine("");

        for (int i = 0; i < 10; i++)
        {
            for (int j = 1; j < 10; j++)
            {
                if (numbers[i] == numbers[j])
                {
                    break;
                }
                else
                {
                    Console.Write(numbers[i] + ", ");
                }
            }
        }
Cœur
  • 37,241
  • 25
  • 195
  • 267
Yummy275
  • 293
  • 1
  • 11
  • What do you want when write this program, my friend? – Tomato32 Jun 12 '17 at 02:31
  • I just want to print out the unique elements in a random array. For example, if the array was 1,4,6,6,6,3,3 , I want the program to output 1,3,4,6. That foreach part is just so i can compare and make sure its working right. – Yummy275 Jun 12 '17 at 02:35
  • `numbers.Distinct()` is not an option? – Maxim Jun 12 '17 at 02:36
  • It seems to me that the code you posted will print numbers too early, before you know whether they are unique or not. It also seems to me that, since you sort the values first, you should not need a nested loop to find duplicates. Have you made any attempt to debug this yourself? Have you noticed anything specifically wrong? What _specifically_ are you having trouble with? – Peter Duniho Jun 12 '17 at 02:36
  • Create a dictionary and add the numbers to a dictionary if not added already. Then loop the dictionary and print out the numbers. Search for c# dictionary online. – CodingYoshi Jun 12 '17 at 02:40
  • Your question is going to attract a lot of low-quality answers. The FGITW types can't resist. See marked duplicates for a number of options for removing duplicates. Your question does not provide enough context to know what's allowed here and what's not (e.g. can you just call `Distinct()`?). If LINQ is not allowed, then the best approach is to sort (like you are now) and then scan the array once, keeping track of the most recent value, and skipping any value that's the same as the most recent one. You can see examples of that and other approaches in the marked duplicates. – Peter Duniho Jun 12 '17 at 02:44
  • Sorry for the bad question, after looking it over I realize I didn't even ask a question really and didn't give much context lol. But no , Distinct() is not allowed. and Thank you @PeterDuniho those duplicate questions and your thoughts helped me out a lot! – Yummy275 Jun 12 '17 at 02:52

1 Answers1

0

I'm not sure what that craziness with the nested for loops at the end is doing. but you can do it like this.

        int[] numbers = new int[10];
        Random random = new Random();

        for (int i = 0; i < 10; i++)
        {
            numbers[i] = random.Next(0, 10);
        }

        Array.Sort(numbers);
        foreach (var num in numbers)
        {
            Console.Write("{0}, ", num);
        }
        Console.WriteLine();
        numbers = numbers.Distinct().ToArray();
        numbers.ToList().ForEach(x => Console.Write(x + ", "));

Another option that is shorter, I got it down to 4 lines:

       Random random = new Random();
       int[] numbers = Enumerable.Repeat(0, 10).Select(i => random.Next(0, 10)).ToArray();
       Console.WriteLine(string.Join(", ", numbers));
       Console.WriteLine(string.Join(", ", numbers.Distinct()));
Alex
  • 552
  • 3
  • 15