Подскажите пожалуйста как ее решить...может через массив?

var mas:array[0..9] of byte;
a,i,min,max:integer;
begin
for i:=0 to 9 do mas[i]:=0;
repeat
readln(a);
if (a<>-1) then inc(mas[a]);
until (a=-1);
min:=mas[0];max:=mas[0];
for i:=1 to 9 do
begin
if (min>mas[i]) then min:=mas[i];
if (max<mas[i]) then max:=mas[i];
end;
for i:=0 to 9 do
begin
if (min=mas[i]) then writeln('Reje vseh vstrechaetsya chislo ',i);
if (max=mas[i]) then writeln('Chaje vseh vstrechaetsya chislo ',i);
end;
readln;
end.
var mas:array[0..9] of byte;
a:string;
i,min,max,k,er:integer;
begin
for i:=0 to 9 do mas[i]:=0;
repeat
readln(a);
if (a<>'-1') then for i:=1 to length(a) do begin
val(a[i],k,er);
inc(mas[k]);
end;
until (a='-1');
min:=mas[0];max:=mas[0];
for i:=1 to 9 do
begin
if (min>mas[i]) then min:=mas[i];
if (max<mas[i]) then max:=mas[i];
end;
for i:=0 to 9 do
begin
if (min=mas[i]) then writeln('Reje vseh vstrechaetsya chislo ',i);
if (max=mas[i]) then writeln('Chaje vseh vstrechaetsya chislo ',i);
end;
readln;
end.
varТут я постарался избежать сортировки массива в чистом виде (и это избавило прогу от одного лишнего цикла и дополнительного массива). Кроме прочего, использование индекса массива и цикла типа char, надеюсь, полезно для обучения. И вообще, тут несколько хитростей, некоторые из них немного спорные.. ))
s: string;
i: integer;
Stat: array[' '..'9']of integer;
c,max: char;
begin
FillChar(Stat,SizeOf(Stat),#0);
repeat
ReadLn(s);
for i:=1 to Length(s) do Inc(Stat[s[i]]);
until s='-1';
Dec(Stat['1']);
repeat
max:=' ';
for c:='0' to '9' do if Stat[c]>Stat[max] then max:=c;
Stat[max]:=0;
WriteLn(max)
until max=' ';
ReadLn
end.