Haseeb Ahmed
Haseeb Ahmed

Reputation: 37

c# Remove duplicates from an Array

int[] RandArray = new int[6];

        Random randNumber = new Random();
        for (int Counter = 0; Counter < RandArray.Length; Counter++)
        {
            RandArray[Counter] = randNumber.Next(1,50);

        }

        Console.WriteLine(RandArray[0]);
        Console.WriteLine(RandArray[1]);
        Console.WriteLine(RandArray[2]);
        Console.WriteLine(RandArray[3]);
        Console.WriteLine(RandArray[4]);
        Console.WriteLine(RandArray[5]);
        Console.ReadLine();

This program generates 6 Random numbers from 1 to 49. Presently, it generates the same numbers twice. How do I make it not duplicate any number? And can this code be improved?

Upvotes: 0

Views: 140

Answers (4)

Yacoub Massad
Yacoub Massad

Reputation: 27861

You can use a HashSet to store the numbers that you have generated so far. The HashSet will only add the newly generated number if it does not contain the number already. Like this:

Random rand_number = new Random();

HashSet<int> numbers = new HashSet<int>();

while (numbers.Count < 6)
{
    int new_number = rand_number.Next(1, 50);

    numbers.Add(new_number);
}

You can use a List instead of the HashSet if you want, but a HashSet will perform sigificantly better if the number of integers to generate is big.

You can convert the result into an array after you finish (if you want), like this:

int[] integer_array = numbers.ToArray();

Instead, you can do this if you want the numbers to be ordered ascending:

int[] integer_array = numbers.OrderBy(x => x).ToArray();

Upvotes: 2

Stuart
Stuart

Reputation: 764

You could create a range of numbers from 1 to 50, order them randomly and then 6 from that range into a list:

var rnd = new Random();
var randomNumbers = Enumerable.Range(1,50).OrderBy(x => rnd.Next()).Take(6).ToList();

.NET Fiddle - https://dotnetfiddle.net/i06zCY

Upvotes: 1

Eugene Krapivin
Eugene Krapivin

Reputation: 1861

This answers your title question.
Use Distinct LINQ operator, it will remove the duplicate elements https://msdn.microsoft.com/en-us/library/vstudio/bb348436(v=vs.100).aspx

This answers your body question
To not generate same number multiple times store the numbers in a HashSet and check each iteration if number was generated previously and re-roll if needed.

Upvotes: 1

Guffa
Guffa

Reputation: 700302

You can check for a duplicate for each number that you create:

int[] RandArray = new int[6];

Random randNumber = new Random();
for (int Counter = 0; Counter < RandArray.Length; Counter++) {
  // Loop until you find an unused number
  bool found;
  do {
    // Get a random number
    int num = randNumber.Next(1,50);
    // Look through the numbers picked so far
    found = false;
    for (int i = 0; i < Counter; i++) {
      found |= num == RandArray[i];
    }
    // If the number was found go back and pick a new one
  } while(found);
  // Put the number in the array
  RandArray[Counter] = num;
}

foreach (int n in RandArray) {
  Console.WriteLine(n);
}
Console.ReadLine();

Upvotes: 0

Related Questions