Use count tables for centerpiece example
This commit is contained in:
parent
d812b28505
commit
ffafafa239
1 changed files with 3 additions and 10 deletions
|
|
@ -11,18 +11,11 @@ Getting started? Head over to the [installation instructions](/getting_started/)
|
|||
``` nimrod
|
||||
import tables, strutils
|
||||
|
||||
var wordFrequencies = initTable[string, int]()
|
||||
var wordFrequencies = newCountTable[string]()
|
||||
|
||||
for line in stdin.lines:
|
||||
for word in line.split(", "):
|
||||
wordFrequencies[word] = wordFrequencies[word] + 1
|
||||
wordFrequencies.inc(word)
|
||||
|
||||
var maxFrequency = 0
|
||||
var mostFrequentWord: string
|
||||
for word, frequency in wordFrequencies:
|
||||
if frequency > maxFrequency:
|
||||
maxFrequency = frequency
|
||||
mostFrequentWord = word
|
||||
|
||||
echo "The most frequent word is '", mostFrequentWord, "'"
|
||||
echo "The most frequent word is '", wordFrequencies.largest, "'"
|
||||
```
|
||||
Loading…
Add table
Add a link
Reference in a new issue