Overview

Frequency Counting

Demonstrates a classic use case for associative arrays. This example showcases word frequency analysis by combining string splitting with automatic initialization of dictionary values, followed by iteration over the 'Keys' collection.

Source Code

var text := 'the quick brown fox jumps over the lazy dog';
var words := text.Split(' ');
var counts: array [String] of Integer;

for var w in words do
  counts[w] := counts[w] + 1;

PrintLn('Word counts:');
for var w in counts.Keys do begin
  PrintLn('  ' + w + ': ' + IntToStr(counts[w]));
end;

Result

Word counts:
  over: 1
  jumps: 1
  dog: 1
  quick: 1
  lazy: 1
  brown: 1
  the: 2
  fox: 1
On this page