Checking consecutive characters in a string First of all, this code will ask for a number (first) on how many test the user would like to have. Then the user will input the strings depending on how the number entered.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim input As String = txtInput.Text
    Dim inputArr As String() = input.Split(New Char() {Environment.NewLine})
    Dim count As Integer = Convert.ToInt32(inputArr(0))
    If count &gt;= 1 And count &lt;= 10 Then
        If txtInput.Lines.Length() - 1 &lt;= count Then
            For x As Integer = 1 To count
                If inputArr(x).Length - 1 &gt;= 1 And inputArr(x).Length - 1 &lt;= 10 Then
                    Dim numDeletes = CountConsecutiveDuplicates(inputArr(x))
                    txtResult.AppendText(numDeletes &amp; Environment.NewLine)
                Else
                    MessageBox.Show("Hell no!")
                End If
            Next x
        Else
            MessageBox.Show("Max test reached!")
        End If

    Else
        MessageBox.Show("Number of cases is limited only from 1 - 10")
    End If
End Sub

Shared Function CountConsecutiveDuplicates(Of T)(input As IEnumerable(Of T)) As Int32
    Dim count As Int32 = 0
    Dim comparer = EqualityComparer(Of T).Default

    ' optimization for lists and arrays: '
    Dim listT = TryCast(input, IList(Of T))
    If listT IsNot Nothing Then
        If listT.Count &lt;= 1 Then Return 0
        For i As Int32 = 0 To listT.Count - 2
            If comparer.Equals(listT(i), listT(i + 1)) Then
                count += 1
            End If
        Next
        Return count
    End If

    If Not input.Any() Then Return 0
    Dim this As T = input.First()
    For Each item As T In input.Skip(1)
        If comparer.Equals(this, item) Then
            count += 1
        End If
        this = item
    Next
    Return count
End Function


Here are the constraints:


tests &gt;= 1 and tests &lt;= 10
charofstring &gt;= 1 and charofstring &lt;= 10


This code will check the string that user input in a multiline textbox. For example, the input is 4 (as the test counter)(\n) AABBCCC (first string to check)(\n) AAAABBBC (2nd string to check)(\n) ... (4th string)

I need to check on the number of consecutive characters that are needed to delete until no characters are consecutive. This will be the result of the user input.


4 (first string because 1A 1B and 2C) - text in the parentheses not included in output
5 (2nd string because 3A 2B)
...


Is there any code that I do not need? Someone told me that I can still shorten the code I have. How can I modify this code?


"txtInput = name of input textbox"
"txtResult = name of output textbox"
"Button1 = name of button to execute the code."
By using the Distinct method and comparing lengths you can get the number you want in one line.  Here's what such a function could look like:

Function CharsToDelete(Of T)(input As IEnumerable(Of T)) As Integer
    Return input.Count - input.Distinct.Count
End Function


Oops missed the part about consecutive chars.  This should do the trick:

Function CharsToDelete(Of T)(input As IEnumerable(Of T)) As Integer
    Return input.Where(Function(x, y) x.Equals(input(y + 1))).Count
End Function


You don't need to split the text into an array.  The Lines property of the TextBox already does this:

Dim inputArr As String() = txtInput.Lines