So far all proposals seem to be O(N^2) and I cannot think of anything that has not a worst-case performance of that order. Stealing VinsWorldcoms example, one implementation would be

use strict; use warnings; my @students = ( [qw(cap shortsleeve jeans leather flipflop)], [qw(beenie longsleeve shorts cloth highheel)], [qw(cap tanktop shorts rope flat)], [qw(beenie longsleeve shorts cloth flipflop)] ); my @best = ( -1 ); while( my $x = shift @students ) { Y: for my $y (@students) { my $diff = @$x; for( my $i = 0; $i<@$x; $i++ ) { $diff-- if $x->[$i] eq $y->[$i]; next Y if $diff <= $best[0]; } @best = ( $diff, $x, $y ); } } print "@$_\n" for @best[1,2];

Not sure this is an improvement over what you have already.

I am still thinking whether any kind of sorting will improve performance. The aim would be to trigger the shortcut from the loop most often. It exits early if two arrays are similar but ONLY if one has already encountered two very different ones. My intuition is to say, one should put the columns with the least variation first, and also the rows with the most frequent items. But I cannot get to grips why this should be so....


In reply to Re: Help thinking about an alternate algorithm by hdb
in thread Help thinking about an alternate algorithm by Limbic~Region

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.