Performance wise the hash method will always take O(n) time, even if the first and second elements of the lists are different. An appriach like what I present below can bail-out if it finds a mismatch before examining the entire list. Therefore giving it a better average and best-case performance for lists that don't have homogeneous contents.
This is not as short and cool, but does have better algorithmic performance. You could tweak-out the overhead I incurred by copying the list if you wanted the utmost in speed. My technique is probably slightly slower worse-case (a homogeneous list), because it does do slightly more work.sub is_list_homegeneous{ my @list=@_; my $val=shift @list; foreach(@list){ return 0 if($_ ne $val) } return 1; }
In reply to RE: RE: RE: RE: Re: All array elements the same?
by lhoward
in thread All array elements the same?
by Grumpy
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |