I agree, except in your assertion that the hash brute-force method is best.

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.

sub is_list_homegeneous{ my @list=@_; my $val=shift @list; foreach(@list){ return 0 if($_ ne $val) } return 1; }
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.

In reply to RE: RE: RE: RE: Re: All array elements the same? by lhoward
in thread All array elements the same? by Grumpy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.