Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,936 questions

51,873 answers

573 users

How to find the most common pair of characters in a string with VB.NET

1 Answer

0 votes
Imports System
Imports System.Collections.Generic

Public Class MostCommonPair_VB_NET
    Public Shared Sub Main(ByVal args As String())
        Dim s As String = "xzvxdeshaalzxzmdenlopxzxzxzaaqdewrzaaaapeerxzxz"
		
        Console.WriteLine("The most common pair: " & FindMostCommonPair(s))
    End Sub

    Public Shared Function FindMostCommonPair(ByVal s As String) As String
        Dim pairCount As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer)()

        For i As Integer = 0 To s.Length - 1 - 1
            Dim pair As String = s.Substring(i, 2)

            If pairCount.ContainsKey(pair) Then
                pairCount(pair) += 1
            Else
                pairCount(pair) = 1
            End If
        Next

        Dim mostCommonPair As String = Nothing
        Dim maxCount As Integer = 0

        For Each entry In pairCount
            If entry.Value > maxCount Then
                mostCommonPair = entry.Key
                maxCount = entry.Value
            End If
        Next

        Console.WriteLine("Max count: " & maxCount)
        Return mostCommonPair
    End Function
End Class



' run:
'
' Max count: 7
' The most common pair: xz
'

 



answered Nov 27, 2024 by avibootz

Related questions

...