Compare arrays as (multi-) sets

前端 未结 8 1699
梦如初夏
梦如初夏 2021-01-04 21:42

I\'m looking for an efficient way to find out whether two arrays contain same amounts of equal elements (in the == sense), in any order:

foo = {         


        
8条回答
  •  离开以前
    2021-01-04 21:55

    i wasn't sure if "===" is ok, the question is a bit vauge... if so, this is quite a bit faster and simpler than some other possible ways of doing it:

    function isSame(a,b){
      return a.length==b.length && 
          a.filter(function(a){ return b.indexOf(a)!==-1 }).length == b.length;
    }
    

提交回复
热议问题