• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1367
  • Last Modified:

Remove duplicates in random number generator

I am confused on how to use LINQ and remove duplicate numbers from  my random number generator below is the code that I have written
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Cunningham_CSC153_A13_P2
{
    class Program
    {
        static void Main(string[] args)
        {
            Random randomNumber = new Random();

            int[] num;
            num = new int[20];

            List<int> ranNum = new List<int>();

            for(int count = 0; count <= 29; count++)
            {
                //List<int> ranNum = new List<int>();
                ranNum.Add(randomNumber.Next(1,20));
                

               
               
            }
            var sortRanNum =
                 from value in ranNum
                 orderby value
                 select value;

            Display(sortRanNum, "Random Numbers sorted in ascending order");

            Console.WriteLine();

            var sortDecRanNum =
                 from value in ranNum
                 orderby value descending
                 select value;

            Display(sortDecRanNum, "Random Numbers sorted in descending order");

            Console.WriteLine();

            var duplicatesRemoved =
                 from value in ranNum
                 orderby value 
                 select value;

            Display(duplicatesRemoved, "Random Numbers and duplicates removed");



           
               


        }
        public static void Display<T>(IEnumerable<T> results, string header)
        {
            Console.WriteLine("{0}: ", header);
            foreach (T element in results)
                Console.WriteLine(element.ToString());

            Console.WriteLine();
        }
    }
}

Open in new window

0
rcham68
Asked:
rcham68
  • 9
  • 6
1 Solution
 
p_davisCommented:
ranNum = ranNum.Distinct();
0
 
rcham68Author Commented:
Exactly where does this go?  Here is where I put it but it does not work

 var duplicatesRemoved =
                 from value in ranNum
                 orderby value
                 select ranNum = ranNum.Distinct();
           
            Display(duplicatesRemoved, "Random Numbers and duplicates removed");
0
 
p_davisCommented:
I would put it right after the for loop.

 for(int count = 0; count <= 29; count++)
            {
                //List<int> ranNum = new List<int>();
                ranNum.Add(randomNumber.Next(1,20));
               
            }
ranNum = ranNum.Distinct();
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
rcham68Author Commented:
Can not put it there it gives me an error explicit conversion exist
0
 
p_davisCommented:
just cast the distinct to (List<int>)
0
 
p_davisCommented:
sorry, that doesn't work....
0
 
p_davisCommented:
this worked

(from num in ranNum
                 select num).Distinct().ToList();
0
 
p_davisCommented:
ranNum = (from num in ranNum
                 select num).Distinct().ToList();
0
 
rcham68Author Commented:
Thanks
0
 
p_davisCommented:
np, sorry about the false ones -- wasn't able to test right away
0
 
rcham68Author Commented:
ranNum = (from num in ranNum
                 select num).Distinct().ToList();

Where did you put this? Every where I put it I am getting a conflict with previous declaration of num
0
 
rcham68Author Commented:
ranNum = (from num in ranNum
                 select num).Distinct().ToList();

Where did you put this? Every where I put it I am getting a conflict with previous declaration of num
0
 
p_davisCommented:
num was something that was unique to my testing program -- change the name

ranNum = (from n in ranNum
                 select n).Distinct().ToList();
0
 
p_davisCommented:
after the for
0
 
rcham68Author Commented:
Got it working thanks a lot.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

  • 9
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now