# Thread: Prevent Duplicates in Array

1. ## Prevent Duplicates in Array

How do I prevent duplicate values in an Array?

The elements of my Array are added by generating random numbers when I click a button on my form but shouldn't have the same value more than once.

I have seen code online for removing duplicates but that changes the size of the Array and I need it to remain the same size as it is used to populate 6 labels on my form.

This is the code I am using to fill my array.

Code:
```Dim Lotto(6) As Integer
Dim RndLotto As New Random
Dim X As Integer

For X = 1 To UBound(Lotto)
Lotto(X) = RndLotto.Next(1, 59)
Next```

2. ## Re: Prevent Duplicates in Array

Assuming this is homework, the expected solution likely is to:

- Generate the random number and store it in a temporary variable
- Pass that random number to a function that checks the array to see if that number is already in the array
-- If it is already in the array, generate a new random number, store it in that temporary variable, and repeat the previous step
-- If it is not already in the array, then assign it to the lotto array

Good luck.

3. ## Re: Prevent Duplicates in Array

There were a couple of points i noticed. Check the comments...

Code:
```Dim Lotto(5) As Integer ' if you want a 6 element array
Dim RndLotto As New Random

For x As Integer = 0 To Lotto.GetUpperBound(0) ' LBound to ubound
Dim n As Integer = RndLotto.Next(1, 60) ' random number 1 to 59
If Not Lotto.Contains(n) Then ' ensure unique numbers
Lotto(x) = n
Else
x -= 1
End If
Next```

4. ## Re: Prevent Duplicates in Array

Originally Posted by .paul.
There were a couple of points i noticed. Check the comments...

Code:
```Dim Lotto(5) As Integer ' if you want a 6 element array
Dim RndLotto As New Random

For x As Integer = 0 To Lotto.GetUpperBound(0) ' LBound to ubound
Dim n As Integer = RndLotto.Next(1, 60) ' random number 1 to 59
If Not Lotto.Contains(n) Then ' ensure unique numbers
Lotto(x) = n
Else
x -= 1
End If
Next```
Thanks. That works perfectly.

5. ## Re: Prevent Duplicates in Array

How about you don't allow duplicates in the first place? Then there's no need to prevent them being placed in the array. How does a real lottery draw work? It puts all the possible values into a container and then removes items at random from that container. Why can't you do that?

Alternatively, you could treat it like dealing cards, i.e. randomise the whole list and then remove the first item repeatedly.

6. ## Re: Prevent Duplicates in Array

Shuffling the full 59 numbers and taking the first 6 would be something like this..

Code:
```Dim Lotto() As Integer
Dim allNumbers() As Integer = Enumerable.Range(1, 59).ToArray
Dim RndLotto As New Random
allNumbers = allNumbers.OrderBy(Function(x) RndLotto.NextDouble).ToArray
Lotto = allNumbers.Take(6).ToArray```

7. ## Re: Prevent Duplicates in Array

Originally Posted by .paul.
Shuffling the full 59 numbers and taking the first 6 would be something like this..

Code:
```Dim Lotto() As Integer
Dim allNumbers() As Integer = Enumerable.Range(1, 59).ToArray
Dim RndLotto As New Random
allNumbers = allNumbers.OrderBy(Function(x) RndLotto.NextDouble).ToArray
Lotto = allNumbers.Take(6).ToArray```
No point calling ToArray multiple times. Only the last one is needed.

8. ## Re: Prevent Duplicates in Array

Originally Posted by jmcilhinney
No point calling ToArray multiple times. Only the last one is needed.
That would mean…

Code:
`Dim allNumbers() As Integer`
Would have to be changed to…

Code:
`Dim allNumbers`
Which would be an ienumerable of some undefined type

9. ## Re: Prevent Duplicates in Array

Or you could just do it all in one chain:
vb.net Code:
```Dim rng As New Random
Dim selectedNumbers = Enumerable.Range(1, 58).OrderBy(Function(n) rng.NextDouble()).Take(6).ToArray()```
Note that the original code used Random.Next(1, 59), so numbers in the range 1-58. Enumerable.Range takes the inclusive minimum and the count, rather than the inclusive minimum and the exclusive maximum, so you need to use 58 rather than 59.

10. ## Re: Prevent Duplicates in Array

👍 That’d work. But I thought the original random numbers were 1-58 and should’ve been 1-59 inclusive

11. ## Re: Prevent Duplicates in Array

You could use a HashSet, add values than use ToArray on the HashSet. In the example below is a proof of concept, you can add in the random values while written as shown below you know what the input and results are.

For a console app, the HashSet should be in Main but for a Forms project working with a button you need this for the add and the result.

Code:
```Module Program
Private ReadOnly _hashSet As New HashSet(Of Integer)()

Sub Main(args As String())

Dim randomValues() As Integer = { 1, 1, 2, 3, 4, 5, 6, 6 }

For Each value In randomValues
Next

Dim values() As Integer = _hashSet.ToArray()
Console.WriteLine(String.Join(",", values))

End Sub
End Module```

12. ## Re: Prevent Duplicates in Array

Originally Posted by kareninstructor
You could use a HashSet, add values than use ToArray on the HashSet. In the example below is a proof of concept, you can add in the random values while written as shown below you know what the input and results are.

For a console app, the HashSet should be in Main but for a Forms project working with a button you need this for the add and the result.

Code:
```Module Program
Private ReadOnly _hashSet As New HashSet(Of Integer)()

Sub Main(args As String())

Dim randomValues() As Integer = { 1, 1, 2, 3, 4, 5, 6, 6 }

For Each value In randomValues
Next

Dim values() As Integer = _hashSet.ToArray()
Console.WriteLine(String.Join(",", values))

End Sub
End Module```
This is definitely the right idea but that algorithm has a slight problem. OP implies that he wants to have an array of a specific size, 6 items in his case. Depending on how the random numbers are generated and fed into the HashSet, it could end up with less than 6 items if there were too many duplicates. I'd use your idea but with a different twist:-
Code:
```        Dim lotto As Integer() = New Func(Of Integer())(Function()
Dim hs As New HashSet(Of Integer)
Dim r As New Random

Do Until hs.Count = 6
Loop
Return hs.ToArray
End Function).Invoke

Debug.WriteLine(String.Join(", ", lotto))```
The above code guarantees 2 things, that the array will always be 6 elements long and that each number is unique.

13. ## Re: Prevent Duplicates in Array

Originally Posted by jmcilhinney
Or you could just do it all in one chain:
vb.net Code:
```Dim rng As New Random
Dim selectedNumbers = Enumerable.Range(1, 58).OrderBy(Function(n) rng.NextDouble()).Take(6).ToArray()```
Note that the original code used Random.Next(1, 59), so numbers in the range 1-58. Enumerable.Range takes the inclusive minimum and the count, rather than the inclusive minimum and the exclusive maximum, so you need to use 58 rather than 59.
that is pretty cool JMC,
here another twist based on your Idea
Code:
```Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
MessageBox.Show(String.Join("-", Enumerable.Range(1, 58).OrderBy(Function(x) Guid.NewGuid()).Take(6).OrderBy(Function(x) x)))
End Sub```

14. ## Re: Prevent Duplicates in Array

Originally Posted by Niya
This is definitely the right idea but that algorithm has a slight problem.
True, I was not going there, figured my reply was to inject an alternate idea and leave the rest to the OP.

15. ## Re: Prevent Duplicates in Array

Originally Posted by kareninstructor
True, I was not going there, figured my reply was to inject an alternate idea and leave the rest to the OP.
Fair enough.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•