Created
October 25, 2011 14:38
-
-
Save soney/1312954 to your computer and use it in GitHub Desktop.
Order Agnostic Array Equivalence Check
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var order_independent_equality_check = function(arr1, arr2, isEqual) { | |
if(arr1.length !== arr2.length) { return false; }; | |
isEqual = isEqual || function(a,b){return a === b;}; | |
var contains = function(arr, obj, equalityCheck, ignore_indicies) { | |
equalityCheck = equalityCheck || function(a,b){return a===b;}; | |
ignore_indicies = ignore_indicies || []; | |
for(var i = 0, len = arr.length; i<len; i++) { | |
var ignore = false; | |
for(var j = 0, lenj = ignore_indicies.length; j<lenj; j++) { | |
if(ignore_indicies[j] === i) { | |
ignore = true; | |
break; | |
} | |
} | |
if(!ignore) { | |
if(equalityCheck(arr[i], obj)) { return i; } | |
} | |
} | |
return false; | |
}; | |
var used_arr2_indicies = []; | |
for(var i = 0, len = arr1.length; i<len; i++) { | |
var equiv_obj_index = contains(arr2, arr1[i], isEqual, used_arr2_indicies); | |
if(equiv_obj_index === false) { return false; } | |
else { | |
used_arr2_indicies.push(equiv_obj_index); | |
} | |
} | |
return true; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment