Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Assuming you have one number in each string:</p> <pre><code>Dim myNumbers As String() = New String() {"9", "9", "8", "8", "8", "8", "7", "7", "6", "5"} 'NumberCounts will hold the counts of each number Dim numberCounts As New System.Collections.Generic.Dictionary(Of String, Integer) For Each number As String In myNumbers If numberCounts.ContainsKey(number) Then 'If we've alreade seen this number before, add one to the count numberCounts(number) += 1 Else 'If it's the first time, add it to the list. numberCounts.Add(number, 1) End If Next number ' So now we have the counts of all the numbers, let's sort them Dim sortedCount As New System.Collections.Generic.Dictionary(Of String, Integer) 'Keep going until there are no more numbers in the numberCount list. Do Until numberCounts.Count = 0 'Find the most occurring number Dim maxNumberSoFar As New System.Collections.Generic.KeyValuePair(Of String, Integer)("", 0) For Each numberCount As System.Collections.Generic.KeyValuePair(Of String, Integer) In numberCounts If numberCount.Value &gt; maxNumberSoFar.Value Then 'Ha! This number occurres more frequently than the the one we already found maxNumberSoFar = numberCount End If Next numberCount 'So now we know that we have the most frequently occurring number. ' Add it to the results sortedCount.Add(maxNumberSoFar.Key, maxNumberSoFar.Value) ' Remove it from the numberCount so that we don't count it again numberCounts.Remove(maxNumberSoFar.Key) Loop 'Show the results! Console.WriteLine("Numbers have been sorted, most frequently to least:") For Each displayNumber As System.Collections.Generic.KeyValuePair(Of String, Integer) In sortedCount Console.WriteLine("Number " &amp; displayNumber.Key &amp; " occurs " &amp; displayNumber.Value &amp; " times.") Next displayNumber </code></pre> <p>This will also work with any strings, not just numbers. If your numbers are not strings, just use .ToString on them, or change all of the <em>(Of String, Integer)</em> to <em>(Of Long, Integer)</em></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload