求找到最大间隔的算法

求找出最大间隔的算法请教一下各位,我有一个数组{0,1,2,3,4,5,6,7,8,9,0,1,5,6,9.....}很多的数据,我要找

求找出最大间隔的算法
请教一下各位,我有一个数组
{0,1,2,3,4,5,6,7,8,9,0,1,5,6,9.....}
很多的数据,我要找出某一个数,比如2的最大间隔,用什么方法最快

[解决办法]
假设list是储存的数组:


function max(list;num:integer): Integer;
var
  i,j, pre: integer;
begin
  j:=0;
  pre:=-1;
  for i:= 1 to list.len do
    if list[i] = num then
      if (pre<>-1) and ((i-pre)>j) then
        j := i-pre;
  result := j;
end;