/ Published in: JavaScript
listsEqual(["1","2"], ["1","2"]) is true;
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
var listsEqual = function(list1, list2){ var differences = 0; // check if all elements from first list exists in the second for(var i=0; i<list1.length;i++){ var exists = false; for(var j=0; j<list2.length; j++){ if(list1[i] == list2[j]){ exists = true; break; } } if(!exists){ differences++; break; } } // and vice versa if(differences == 0){ for(var i=0; i<list2.length;i++){ var exists = false; for(var j=0; j<list1.length; j++){ if(list2[i] == list1[j]){ exists = true; break; } } if(!exists){ differences++; break; } } } return differences == 0; }