# Flexible Count 2 digits • ### Question

• HI All,

I have some data line from text file;

3096,9575,5857,7523,2939,8257,1090,7786,3978,2445,5678,6082,5068,3348,3116,0567,5330,3163,8051,1694,6228,0958,0520
1252,7456,2918,7195,5261,3915,9565,1349,1809,7841,1955,6685,9474,3382,0878,0215,0475,5198,6304,0340,4132,7968,4855

what I want to do is How to count Occurances two digit and show into the textbox.
Example.
First we found digit 30 for 3096 is 1

found digit 30 for 5330 is 2

second we found digit 30 for 6304 is 1
Third we found digit 30 for 0340 is 2 because we don't care about position. 03 and 30 it same.
Then total count occurances for 30 is 6.
After finish count digit 30.If we found again digit 30 then skip count.
Next,
Count digit 09 for 3096 is 1
Count digit 09 for 1090 is 2
Count digit 09 for 0958 is 1
Count digit 09 for 1809 is 1
total is 5
next,
Count digit 96 for 3096 is 1
Count digit 96 for 1694 is 1
Count digit 96 for 9565 is 1
Count digit 96 for 7968 is 1
total is 4
next,
Count digit 06 for 3096 is 1
Count digit 06 for 6082 is 1
Count digit 06 for 5068 is 1
Count digit 06 for 0567 is 1
Count digit 06 for 6304 is 1
total is 5
next,
Count digit 39 for
........
........
Total is
next,
Count digit 36 for
........
........
Total is

After finish 3096 then go to the 9575

Count digit 95 for 9575 is 1
Count digit 95 for 0958 is 1
Count digit 95 for 7195 is 1
Count digit 95 for 9565 is 2
Count digit 95 for 1955 is 2
Count digit 95 for 5198 is 1
total is 8
next,
Count digit 57 for
........
........
Total is

Test this code but less count.

```Option Strict On
Option Explicit On
Option Infer Off

Imports System.IO
Imports System.Data

Public Class Form1

Dim Data As New List(Of String)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Do While sr.Peek <> -1
Dim StringTxt As String = sr.ReadLine()
TextBox1.AppendText(StringTxt & vbCrLf)
Loop

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
For I As Integer = 0 To Data.Count - 1
Dim StringToCheck As String = Data(I).ToString

StringToCheck = StringToCheck.Replace(vbCrLf, ",")
StringToCheck = StringToCheck.TrimEnd(CChar(","))

Dim StringsToCheck As String() = StringToCheck.Split(","c)

'7 0 7 8
'0 1 2 3

For Each S As String In StringsToCheck
Dim StringsToFind As String() = {S(0) & S(1), _
S(1) & S(0), _
S(0) & S(2), _
S(2) & S(0), _
S(0) & S(3), _
S(3) & S(0), _
S(1) & S(2), _
S(2) & S(1), _
S(1) & S(3), _
S(3) & S(1), _
S(2) & S(3), _
S(3) & S(2)}

For Each Find As String In StringsToFind
Dim Count As Integer = 0
For Each Check As String In StringsToCheck
If Check.Contains(Find(0)) AndAlso Check.Contains(Find(1)) Then Count += 1
Next
Me.TextBox2.AppendText("Occurances (" & S & ", " & Find & ")= " & Count.ToString & vbCrLf)
Next
Next
Next

End Sub
End Class```

somebody can modified this code. Or other way more better.

Thank.

• Edited by Tuesday, September 26, 2017 7:13 AM add
Tuesday, September 26, 2017 5:22 AM

### All replies

• what I want to do is How to count Occurances two digit and show into the textbox.

Note that your StringsToFind array is not being created in the sequence of your example - that will make checking your results very difficult.

You need to be able to see what pairs have already been counted so that you do not repeat the pair, but if (0,3) and (3,0) (for instance) are going to be regarded as the same then they need to be adjusted before you can compare them.  You do that by creating a list, ensuring each pair is set to the same standard (eg, lower,higher) and creating a list to check against.

```    Dim Checked As New List(Of String)
For J As Integer = 0 To StringsToFind.Count - 1
Dim Find As String = StringsToFind(J)
If Find(0) > Find(1) Then
Find = StrReverse(Find)
End If
If Not Checked.Contains(Find) Then
Dim Count As Integer = 0
For Each Check As String In StringsToCheck
...```
Then you need to adjust the check so that a value of "0340" (for instance) will return a count of 2 rather than one.  For that code you need to work out how you would do that if you were using pencil and paper.
Tuesday, September 26, 2017 7:29 AM
• Hi mipakteh,

You can take a look the following code:

```For I As Integer = 0 To Data.Count - 1
Dim StringToCheck As String = Data(I).ToString
Dim StringsToCheck As String() = StringToCheck.Split(",")

For Each S As String In StringsToCheck
Dim StringsToFind As String() = {S(0), S(1), S(2), S(3)}
For j As Integer = 0 To StringsToFind.Count - 2
Dim Count As Integer = 0
For Each Check As String In StringsToCheck
If Check.Contains(StringsToFind(j)) AndAlso Check.Contains(StringsToFind(j + 1)) Then Count += 1
Next
Me.TextBox2.AppendText("Occurances (" & S & ", " & StringsToFind(j) & StringsToFind(j + 1) & ")= " & Count.ToString & vbCrLf)
Next
Next
Next```

Best Regards,

Cherry

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

Tuesday, September 26, 2017 9:14 AM
• Hi Acamar,Still get 2 for count digit 30.I need your help.

It should be 6. I think we can put Total of count at the end ,If 30 the count but How Acamar.

Thank.

• Edited by Wednesday, September 27, 2017 11:19 AM add
Wednesday, September 27, 2017 11:02 AM
• thank, but counting not excatly correct

Wednesday, September 27, 2017 11:18 AM
• Hi Acamar,Still get 2 for count digit 30.I need your help.

What is the code you are using instead of

If Check.Contains(StringsToFind(j)) AndAlso Check.Contains(StringsToFind(j + 1)) Then Count += 1

to get the correct count of matches (eg, 2 for (0,3) and "0340")?

Wednesday, September 27, 2017 11:22 AM
• Check this example:

```Dim Data As New List(Of String)

Dim stringToFind = "30"
Dim count = Data.SelectMany(Function(d) d.Split(","c)).Sum(Function(s) Regex.Matches(s, stringToFind(0) & "(?=.*?" & stringToFind(1) & ")|" & stringToFind(1) & "(?=.*?" & stringToFind(0) & ")").Count)
```

Maybe it can be adjusted for your case. Change the value of stringToFind in the loop.

Wednesday, September 27, 2017 11:43 AM
• ```  Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click

For I As Integer = 0 To Data.Count - 1
Dim StringToCheck As String = Data(I).ToString

StringToCheck = StringToCheck.Replace(vbCrLf, ",")
StringToCheck = StringToCheck.TrimEnd(CChar(","))

Dim StringsToCheck As String() = StringToCheck.Split(","c)

For Each S As String In StringsToCheck
Dim StringsToFind As String() = {S(0) & S(1), _
S(1) & S(0), _
S(0) & S(2), _
S(2) & S(0), _
S(0) & S(3), _
S(3) & S(0), _
S(1) & S(2), _
S(2) & S(1), _
S(1) & S(3), _
S(3) & S(1), _
S(2) & S(3), _
S(3) & S(2)}

Dim Checked As New List(Of String)
For J As Integer = 0 To StringsToFind.Count - 1
Dim Find As String = StringsToFind(J)
If Find(0) > Find(1) Then
Find = StrReverse(Find)
End If
If Not Checked.Contains(Find) Then
Dim Count As Integer = 0
For Each Check As String In StringsToCheck
If Check.Contains(Find(0)) AndAlso Check.Contains(Find(1)) Then Count += 1
Next
Me.TextBox3.AppendText("Occurances (" & S & ", " & Find & ")= " & Count.ToString & vbCrLf)
End If
Next
Next
Next

End Sub```

Wednesday, September 27, 2017 2:43 PM
• That code is what you were originally using, but your requirements have changed.   Originally, a value such as 0340 would have counted as one instance of a match, because 0 and 3 both occur.

If Check.Contains(Find(0)) AndAlso Check.Contains(Find(1)) Then Count += 1

"Third we found digit 30 for 0340 is 2 because we don't care about position. 03 and 30 it same."

0340 now counts as 2 matches with 0, 3.  So that line of code needs to change.   You have to work out how you calculate that 0340 should return 2 matches of 0, 3. Then change that line of code to do what you want.

How would you do that if you were using pencil and paper? because that's how you would code it.

Wednesday, September 27, 2017 9:31 PM
• Hi Viorel,

Thank for feedback.

Can you comment or modified to looking good.

```Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
For I As Integer = 0 To Data.Count - 1
Dim StringToCheck As String = Data(I).ToString

StringToCheck = StringToCheck.Replace(vbCrLf, ",")
StringToCheck = StringToCheck.TrimEnd(CChar(","))

Dim StringsToCheck As String() = StringToCheck.Split(","c)

For Each S As String In StringsToCheck
Dim StringsToFind As String() = {S(0) & S(1), _
S(1) & S(0), _
S(0) & S(2), _
S(2) & S(0), _
S(0) & S(3), _
S(3) & S(0), _
S(1) & S(2), _
S(2) & S(1), _
S(1) & S(3), _
S(3) & S(1), _
S(2) & S(3), _
S(3) & S(2)}

For J As Integer = 0 To StringsToFind.Count - 1
Dim Find As String = StringsToFind(J)

Dim count As Integer = Data.SelectMany(Function(d) d.Split(","c)).Sum(Function(n) System.Text.RegularExpressions.Regex.Matches(n, Find(0) & "(?=.*?" & Find(1) & ")|" & Find(1) & "(?=.*?" & Find(0) & ")").Count)

TextBox3.AppendText(CStr(count) & "     " & Find(0) & "   " & Find(1) & "    " & Find(1) & "   " & Find(0) & vbCrLf)
Next

Next
Next

End Sub```

Thursday, September 28, 2017 12:03 PM
• Acamar,I can"t do.I don't how to start.

Sunday, October 1, 2017 6:30 AM
• Acamar,I can"t do.I don't how to start.

If you don't know how to do it with pencil and paper, then you won't be able to code it.   It is a problem in combinations, which you have done several times before, and is also part of the code you originally posted..  See: https://en.wikipedia.org/wiki/Combination

For '0340', for example, the '2 from 4' combinations are:

(0)(1) (0)(2) (0)(3) (1)(2) (1)(3) (2)(3),  or  03 04 00 34 30 40

You need to generate this list, and then decide which items in the list match according to your rule.

• Edited by Sunday, October 1, 2017 6:51 AM sp
Sunday, October 1, 2017 6:50 AM
• Hi Acamar,

``` Dim Data As New List(Of String)
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

Do While sr.Peek <> -1
Dim StringTxt As String = sr.ReadToEnd
TextBox1.AppendText(StringTxt & vbCrLf)
Loop

End Sub

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
For I As Integer = 0 To Data.Count - 1
Dim StringToCheck As String = Data(I).ToString

StringToCheck = StringToCheck.Replace(vbCrLf, ",")
StringToCheck = StringToCheck.TrimEnd(CChar(","))

Dim StringsToCheck As String() = StringToCheck.Split(","c)

'0 3 4 0
'0 1 2 3

For Each S As String In StringsToCheck
Dim StringsToFind As String() = {S(0) & S(1), _
S(0) & S(2), _
S(0) & S(3), _
S(1) & S(2), _
S(1) & S(3), _
S(2) & S(3)}

For Each Find As String In StringsToFind
Dim Count As Integer = 0
For Each Check As String In StringsToCheck
If Check.Contains(Find(0)) AndAlso Check.Contains(Find(1)) Then Count += 1
Next
Me.TextBox2.AppendText("Occurances (" & S & ", " & Find & ")= " & Count.ToString & vbCrLf)
Next

Next
Next
End Sub```

List s(0)s(1) and s(1)s(3) is combination to get 2 match.

How ?.I don't know.

Sunday, November 5, 2017 1:29 PM
• List s(0)s(1) and s(1)s(3) is combination to get 2 match.

Why have you ignored what I proposed?

If (0,3) and (3,0) (for instance) are going to be regarded as the same then they need to be adjusted before you can compare them.  You do that by creating a list, ensuring each pair is set to the same standard (eg, lower,higher) and creating a list to check against. So the code you need will:

(a) examine each pair and, if necessary, change it to a standard layout of (lower value, higher value).
(b) create a list of pairs that have already been counted. Initially it will be empty.
(c) before counting each pair, check if it is already in the list of checked pairs.  If it is, then skip it.  If it isn't, then add it to the list, and count it.

That's how you would do it if you were using pencil and paper.

Sunday, November 5, 2017 10:09 PM