function MostFrequentSymbol(s: string): Char; var i, maxCount: Integer; count: array[Char] of Integer; ch: Char; begin for ch := Low(ch) to High(ch) do count[ch] := 0; for i := 1 to Length(s) do Inc(count[s[i]]); maxCount := 0; for ch := Low(ch) to High(ch) do if count[ch] > maxCount then begin maxCount := count[ch]; MostFrequentSymbol := ch; end; WriteLn('Most frequent symbol is ', MostFrequentSymbol, ', count: ', maxCount); end;
Пример использования:
var s: string; begin s := 'hello world'; MostFrequentSymbol(s); end;
var
i, maxCount: Integer;
count: array[Char] of Integer;
ch: Char;
begin
for ch := Low(ch) to High(ch) do
count[ch] := 0;
for i := 1 to Length(s) do
Inc(count[s[i]]);
maxCount := 0;
for ch := Low(ch) to High(ch) do
if count[ch] > maxCount then
begin
maxCount := count[ch];
MostFrequentSymbol := ch;
end;
WriteLn('Most frequent symbol is ', MostFrequentSymbol, ', count: ', maxCount);
end;
Пример использования:
vars: string;
begin
s := 'hello world';
MostFrequentSymbol(s);
end;
Результат выполнения:
Most frequent symbol is l, count: 3