For Each c As Char In inputString
idx = Asc(c) - startAscii
symbolCount(idx) = symbolCount(idx) + 1
If symbolCount(idx) > symbolCount(maxSymbolIdx) Then
maxSymbolIdx = idx
End If
Next
For idx = 0 To 25
Console.Write(Chr(startAscii + idx) & ": ")
Console.WriteLine(symbolCount(idx).ToString)
Next
Console.WriteLine()
Console.Write("Max Count Symbol: ")
Console.WriteLine(Chr(startAscii + maxSymbolIdx))
End Sub</pre></div><p> </p><h2> </h2><h2>解法二</h2><p> </p><div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:313a96f2-0f63-4ed7-aa9d-e6b0bc28f245" class="wlWriterEditableSmartContent"><pre class="vb:nocontrols" name="code">
Sub Test(ByVal inputString As String)
Dim chars() As Char = inputString.ToCharArray
Array.Sort(chars)
Dim maxChar As Char
Dim maxCount As Integer = 0
Dim preChar As Char = chars(0)
Dim count As Integer = 0
For Each c As Char In chars
If preChar <> c Then
Console.Write(preChar & ": ")
Console.WriteLine(count.ToString)
preChar = c
count = 0
End If
count += 1
If count > maxCount Then
maxCount = count
maxChar = c
End If
Next
Console.WriteLine()
Console.Write("Max Count Symbol: ")
Console.WriteLine(maxChar)
End Sub</pre></div><p> </p><p> </p><p> </p><h2>解法三</h2><p>採用Linq的Group By與Order By來做 <br /> </p><div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:b75086f1-b457-466e-8193-584b39920ead" class="wlWriterEditableSmartContent"><pre class="vb:nocontrols" name="code">
Sub Test(ByVal inputString As String)
Dim chars() As Char = inputString.ToCharArray
Dim linq = From c In chars Group c By c Into Group Order By Group.Count Descending Select New With {.Symbol = c, .Count = Group.Count}
For Each item In linq
Console.WriteLine(item.Symbol & ": " & item.Count)
Next
Console.WriteLine()
Console.Write("Max Count Symbol: ")
Console.WriteLine(linq(0).Symbol)
End Sub</pre></div>