Can be a bit confusing, commented
SCAR Code:
type
TStrTally = record
s: string;
c: Integer;
end;
function TallyStrs(strs: TStringArray): array of TStrTally;
var
t, i, n, h: Integer;
unStrs: TStringArray;
begin
h := High(strs); // high of all strings
SetLength(unStrs, h + 1); // Used to store unique strings
n := -1; // used as the current high for the unique strings
for i := 0 to h do // go through all the strings
begin
for t := 0 to n do // go through all unique strings
begin
if (unStrs[t] = strs[i]) then // if this string matches a unique string
break; // then break as it is not unique
end;
if (t = n + 1) then // if the for loop did not break
begin
n := t; // increase the unique string high
unStrs[n] := strs[i]; // and assigned the string as a new unique string
end;
end;
SetLength(Result, n + 1); // As n is the high, n + 1 is the length used
for t := 0 to n do // loop through all unique strings
begin
Result[t].s := unStrs[t]; // Assign the string to the tally
Result[t].c := 0; // Reset the count
for i := 0 to h do // loop through all the strings
begin
if (Result[t].s = strs[i]) then // if the strings match
Result[t].c := Result[t].c + 1; // then increase the count
end;
end;
end;
var
t: array of TStrTally;
i: Integer;
begin
Writeln('Begin');
t := TallyStrs(['hey', 'wat', 'hey', 'there', 'wat', 'hiya', 'hey']);
for i := 0 to High(t) do
Writeln(t[i].s + ' :' + IntToStr(t[i].c));
Writeln('End');
end.