Solved

Remove duplicates in random number generator

Posted on 2010-11-23
15
1,342 Views
Last Modified: 2012-05-10
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
Comment
Question by:rcham68
  • 9
  • 6
15 Comments
 
LVL 22

Expert Comment

by:p_davis
Comment Utility
ranNum = ranNum.Distinct();
0
 

Author Comment

by:rcham68
Comment Utility
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
 
LVL 22

Expert Comment

by:p_davis
Comment Utility
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
 

Author Comment

by:rcham68
Comment Utility
Can not put it there it gives me an error explicit conversion exist
0
 
LVL 22

Expert Comment

by:p_davis
Comment Utility
just cast the distinct to (List<int>)
0
 
LVL 22

Expert Comment

by:p_davis
Comment Utility
sorry, that doesn't work....
0
 
LVL 22

Expert Comment

by:p_davis
Comment Utility
this worked

(from num in ranNum
                 select num).Distinct().ToList();
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 22

Accepted Solution

by:
p_davis earned 500 total points
Comment Utility
ranNum = (from num in ranNum
                 select num).Distinct().ToList();
0
 

Author Comment

by:rcham68
Comment Utility
Thanks
0
 
LVL 22

Expert Comment

by:p_davis
Comment Utility
np, sorry about the false ones -- wasn't able to test right away
0
 

Author Comment

by:rcham68
Comment Utility
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
 

Author Comment

by:rcham68
Comment Utility
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
 
LVL 22

Expert Comment

by:p_davis
Comment Utility
num was something that was unique to my testing program -- change the name

ranNum = (from n in ranNum
                 select n).Distinct().ToList();
0
 
LVL 22

Expert Comment

by:p_davis
Comment Utility
after the for
0
 

Author Comment

by:rcham68
Comment Utility
Got it working thanks a lot.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now