Re: [flexcoders] What is the best way to compare two arrays element by element ignoring the order?

2008-02-22 Thread Maciek Sakrejda
Extra points for higher-order function, although that is going to be
O(n^2) (unless Array.indexOf() has a weird implementation). If you've
got really, really big arrays (or are doing this in a tight loop) and
you have O(n) memory to spare, you could consider building a hashmap of
the values in Array a, and then walking Array b and checking if each
element is in the hashmap:

var differs:Boolean = (a.length != b.length);
if (!differs) {
  var aContents = new Object();
  a.forEach(function(item:Object, index:int, array:Array):void {
aContents[item] = true;
  });
  differs = b.some(function(item:Object, index:int, array:Array):Boolean
{
return aContents[item] == null;
  });
}

For small-ish arrays, though, I would expect your solution to be faster
than mine (I won't define 'small-ish', since I would honestly be pulling
a number out of my--err, out of thin air).

Also, neither your solution nor mine handles the case where the same
item is in one array twice (yours fails for dupes in a and mine for
dupes in b).

-- 
Maciek Sakrejda
Truviso, Inc.
http://www.truviso.com

-Original Message-
From: Sergey Kovalyov <[EMAIL PROTECTED]>
Reply-To: flexcoders@yahoogroups.com
To: flexcoders@yahoogroups.com
Subject: [flexcoders] What is the best way to compare two arrays element
by element ignoring the order?
Date: Fri, 22 Feb 2008 15:37:56 +0200

What is the best way to compare two arrays element by element ignoring
the order? My solution:

var differs : Boolean =
 (a.length != b.length) ||
 a.some(
  function(item : Object, index : int, array : Array) : Boolean {
   return (b.indexOf(item) == -1);
  });

May be the better solution exists?


 




[flexcoders] What is the best way to compare two arrays element by element ignoring the order?

2008-02-22 Thread Sergey Kovalyov
What is the best way to compare two arrays element by element ignoring the
order? My solution:

var differs : Boolean =
 (a.length != b.length) ||
 a.some(
  function(item : Object, index : int, array : Array) : Boolean {
   return (b.indexOf(item) == -1);
  });

May be the better solution exists?