I want a set of not the same numbers to be added to a listbox, if there are any numbers that are the same, then they won't be added.

I tried:

``````If (Number1 = Number2) Or (Number1 = Number3) Or (Number1 = Number4) Or (Number1 = Number5) = False Then
ListBox1.Items.Add(Number1 & " " & Number2 & " " & Number3 & " " & Number4 & " " & Number5)
End If
``````

However I still got

10 10 50 29 36 13

the first two numbers are of the same value, and yet they somehow managed to get added to the listbox.

How can I fix this?

## All 7 Replies

I found a way, but little bit messy.

``````Dim numLIst As New List(Of Integer)

If Not numLIst.Contains(Number1) Then
End If

If Not numLIst.Contains(Number2) Then
End If

If Not numLIst.Contains(Number3) Then
End If

If Not numLIst.Contains(Number4) Then
End If

If Not numLIst.Contains(Number5) Then
End If

Dim uniqueNums As String = ""
For Each i As Integer In numLIst
uniqueNums = uniqueNums & i & " "
Next

``````

Thank you for the code niranga, however I tried it and it adds the items to the code without the repetetive numbers, however those numbers where there was a repetetive number still gets added.

This is the sample output that I got:
30 6 43 15
13 38 44 45
48 37 19 30 29
14 4 23 73 71

The first two lines are four set of numbers (where the repetetive number was removed)
And the last two are the proper numbers.

So how would I get rid of the set where the repetative number was removed?

We don't give free code niranga, otherwise people don't learn to be able to do it themselves.

A cleaner, faster and more scalable way would be to use a hash (or a bit-array if the size of the numbers are known to be small) to keep track of the numbers that have already been used. That way, you can check if a number is already in the listbox in O(1) time. The insertion of n elements will take O(n) time, at the cost of O(n) auxiliary space (a much better trade off then O(n^2) time and O(1) space).

commented: Thanks a lot for the guidance :) +4

We don't give free code niranga, otherwise people don't learn to be able to do it themselves.

Like I haven't tried at all to do it myself. So what ways do you consider learning then? And it's hard to understand what you said in words, better giving people little crumbs of code rather than philosophical presumptions that don't always help.

``````     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim numberOfSet As Integer = 5
Dim numberOfSlotPerSet As Integer = 5
Dim historyList As New ArrayList
Dim numberDictionary As New System.Collections.Generic.Dictionary(Of Integer, Array)
Dim randomGenerator As New Random

For i As Integer = 0 To numberOfSet - 1

Dim list(numberOfSlotPerSet - 1) As Integer

For x As Integer = 0 To numberOfSlotPerSet - 1
While True
Dim randomNumber As Integer = randomGenerator.Next(10, 99)
If Not historyList.Contains(randomNumber) Then
list(x) = randomNumber
Exit While
End If
End While
Next

Next

Dim msgs As New System.Text.StringBuilder
For Each key As Integer In numberDictionary.Keys
Dim arr As Array = numberDictionary(key)
Dim msg As New System.Text.StringBuilder
For i As Integer = 0 To arr.Length - 1
If msg.Length > 0 Then msg.Append(", ")
msg.Append(arr(i))
Next
msgs.AppendLine(msg.ToString)
Next

MessageBox.Show(msgs.ToString)
Me.Close()
End Sub

Hope that helps!
``````
commented: Thanks for the concept +2

it's hard to understand what you said in words

Use a hash to keep track of which values you have already used so you don't resue them. It's fast, it's clean and it's simple.

I do think your logic is flawed. What happened if the third number is the same as the second and so on? You only test against the first.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.