0

I have 5 numbers and want to test if 3 of them are identical so I wrote this:

Select Case Reel1Num
Case (Reel2Num And Reel3Num) Or (Reel2Num And Reel4Num) Or (Reel2Num And Reel5Num) Or (Reel3Num And Reel4Num) Or (Reel3Num And Reel5Num) Or (Reel4Num And Reel5NUm)
    ThreeNums = +1
    lbl3OfSame.Text = "Three Of Same: " & ThreeNums
End Select

But it didn't seem to return the result I wanted. I know I could use something like:

If (Reel1NUm = Reel2Num And Reel1Num = Reel3Num) Or...   Then
    ThreeNums = +1
    lbl3OfSame.Text = "Three Of Same: " & ThreeNums
End If

But that seems like way more writing than I feel I need to do for the amount that I want to compare.

Is there an easier way to do this?

user692942
  • 16,398
  • 7
  • 76
  • 175

2 Answers2

0

As other commenters have indicated, the 'boolean' comparison of numbers probably isn't doing what you think it's doing. You would need each case to compare its reel value with all other reel values which wouldn't make the syntax much shorter than your If structure. There are various mathematical solutions (and the one proposed by Nathan_Sav's should work if your data is good).

However, you asked if there's a better way and, to keep the code simple, one way would be to assign the reel values to a results array or collection of some kind.

An obvious collection would be a Dictionary which could contain the frequency count and number of each unique reel value. If you had declared your ReelNum as an array then the code would be very short, but keeping your individual variables, you'd need your main Sub and one other to build the Dictionary. So your main Sub would be:

    Dim count As Dictionary(Of Integer, Integer)
    count = New Dictionary(Of Integer, Integer)()

    BuildResults(count, ReelNum1)
    BuildResults(count, ReelNum2)
    BuildResults(count, ReelNum3)
    BuildResults(count, ReelNum4)
    BuildResults(count, ReelNum5)

    Select Case count.Values.Max()
        Case 1 : Console.Write("None the same")
        Case 2 : Console.Write("Two the same")
        Case 3 : Console.Write("Three the same")
        Case 4 : Console.Write("Four the same")
        Case 5 : Console.Write("All the same")
        Case Else : Console.Write("Who knows?")
    End Select

And the building routine:

Private Sub BuildResults(ByRef count As Dictionary(Of Integer, Integer), ByVal newNum As Integer)
    If Not count.ContainsKey(newNum) Then count.Add(newNum, 0)
    count(newNum) = count(newNum) + 1
End Sub

I'm not sure if this is an educational project, but just in case it is, I've included a Select Case so you can see how it works.

Ambie
  • 4,872
  • 2
  • 12
  • 26
  • Thankyou, this did exactly what i wanted. Now to read up on what a dictionary does! Thankyou! – Charlie Turner Mar 07 '17 at 08:26
  • Glad to help. Have a look at all the answers and if you're problem is resolved then please take your post off the unanswered list by 'accepting' one of the answers. – Ambie Mar 07 '17 at 12:12
0

You can group by the numbers and find out which ones appear three times:

Sub Main
    Dim numbers = New List(Of Integer) From {1,2,3,2,4,5,3,6,2,4,7}
    Dim numbersAppearingThreeTimes = numbers.GroupBy(Function(i) i) _
                                            .Where(Function(g) g.Count() = 3)

    For Each g In numbersAppearingThreeTimes
        Console.WriteLine(g.Key)
    Next
End Sub

Result:

2

Chris Dunaway
  • 10,974
  • 4
  • 36
  • 48