Posted By

prudnikov on 02/08/10


Tagged

javascript list array


Versions (?)

Verify if content of 2 lists/arrays are eaqual


 / Published in: JavaScript
 

listsEqual(["1","2"], ["1","2"]) is true;

  1. var listsEqual = function(list1, list2){
  2. var differences = 0;
  3. // check if all elements from first list exists in the second
  4. for(var i=0; i<list1.length;i++){
  5. var exists = false;
  6. for(var j=0; j<list2.length; j++){
  7. if(list1[i] == list2[j]){
  8. exists = true;
  9. break;
  10. }
  11. }
  12. if(!exists){
  13. differences++;
  14. break;
  15. }
  16. }
  17. // and vice versa
  18. if(differences == 0){
  19. for(var i=0; i<list2.length;i++){
  20. var exists = false;
  21. for(var j=0; j<list1.length; j++){
  22. if(list2[i] == list1[j]){
  23. exists = true;
  24. break;
  25. }
  26. }
  27. if(!exists){
  28. differences++;
  29. break;
  30. }
  31. }
  32. }
  33. return differences == 0;
  34. }

Report this snippet  

You need to login to post a comment.