Multiset Equivalence Problem | JavaScript program to check if two given multisets are equivalent ; Create two unordered maps m1 and m2 and insert values of both vectors . ; Now we check if both unordered_maps are same of not . ; Driver code
function areSame ( a , b ) { if ( a . length != b . length ) return false ; var m1 , m2 ; m1 = { } ; m2 = { } ; for ( var i = 0 ; i < a . length ; i ++ ) { if ( m1 . hasOwnProperty ( a [ i ] ) ) { m1 [ a [ i ] ] = m1 [ a [ i ] ] + 1 ; } else { m1 [ a [ i ] ] = 1 ; } if ( m2 . hasOwnProperty ( b [ i ] ) ) { m2 [ b [ i ] ] = m2 [ b [ i ] ] + 1 ; } else { m2 [ b [ i ] ] = 1 ; } } for ( const [ key , value ] of Object . entries ( m1 ) ) { if ( ! m2 . hasOwnProperty ( key ) m2 [ key ] != value ) return false ; } return true ; } var a = [ 7 , 7 , 5 ] ; var b = [ 7 , 7 , 5 ] ; if ( areSame ( a , b ) ) document . write ( " " ) ; else document . write ( " " ) ;