Multiset Equivalence Problem | Java 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
import java . util . * ; class GFG { static boolean areSame ( int [ ] a , int [ ] b ) { if ( a . length != b . length ) return false ; HashMap < Integer , Integer > m1 , m2 ; m1 = new HashMap < Integer , Integer > ( ) ; m2 = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < a . length ; i ++ ) { if ( m1 . containsKey ( a [ i ] ) ) { m1 . put ( a [ i ] , m1 . get ( a [ i ] ) + 1 ) ; } else { m1 . put ( a [ i ] , 1 ) ; } if ( m2 . containsKey ( b [ i ] ) ) { m2 . put ( b [ i ] , m2 . get ( b [ i ] ) + 1 ) ; } else { m2 . put ( b [ i ] , 1 ) ; } } for ( Map . Entry < Integer , Integer > x : m1 . entrySet ( ) ) { if ( ! m2 . containsKey ( x . getKey ( ) ) || m2 . get ( x . getKey ( ) ) != x . getValue ( ) ) return false ; } return true ; } public static void main ( String args [ ] ) { int [ ] a = { 7 , 7 , 5 } ; int [ ] b = { 7 , 7 , 5 } ; if ( areSame ( a , b ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }