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?

Dani AI

Generated

The If in the first post is wrong for two reasons: it only compares other values against Number1 (so duplicates between Number2..Number5 slip through), and the trailing = False does not negate the whole OR expression — it ends up applying only to the last comparison. spotted the first problem; was right to suggest a hash-style check.

A compact, clear way is to collect the five values and test whether all elements are distinct. Using LINQ:

Dim nums = New Integer() {Number1, Number2, Number3, Number4, Number5}
If nums.Distinct().Count() = nums.Length Then
    ListBox1.Items.Add(String.Join(" ", nums))
End If

(Requires Imports System.Linq.)

A slightly lower-level alternative (what meant by a hash) uses a HashSet:

Dim nums = New Integer() {Number1, Number2, Number3, Number4, Number5}
Dim unique As New HashSet(Of Integer)(nums)
If unique.Count = nums.Length Then
    ListBox1.Items.Add(String.Join(" ", nums))
End If

Both approaches test “all five different” before adding. That explains ’s output: their code removes duplicates from a set and then adds the shortened set (hence lines with four numbers). ’s solution, by contrast, enforces uniqueness across all generated numbers using a history — useful only when repeats must be prevented globally. Quick checks: ensure the five values are numeric Integers (not strings with extra spaces) and, if skipping duplicate-containing sets is desired, run one of the uniqueness checks above before adding to the ListBox.

Recommended Answers

All 7 Replies

I found a way, but little bit messy.

Dim numLIst As New List(Of Integer)

If Not numLIst.Contains(Number1) Then
    numLIst.Add(Number1)
End If

If Not numLIst.Contains(Number2) Then
    numLIst.Add(Number2)
End If

If Not numLIst.Contains(Number3) Then
    numLIst.Add(Number3)
End If

If Not numLIst.Contains(Number4) Then
    numLIst.Add(Number4)
End If

If Not numLIst.Contains(Number5) Then
    numLIst.Add(Number5)
End If

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

ListBox1.Items.Add(uniqueNums)

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
                            historyList.Add(randomNumber)
                            list(x) = randomNumber
                            Exit While
                        End If
                    End While
                Next

                numberDictionary.Add(i, list)

            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.