question: public int [ ] intersect ( int [ ] nums1 , int [ ] nums2 ) { Map < Integer , Integer > count = new HashMap < > ( ) ; for ( int n : nums1 ) { count . put ( n , count . containsKey ( n ) ? count . get ( n ) + 1 : 1 ) ; } List < Integer > intersections = new ArrayList < > ( Math . min ( nums1 . length , nums2 . length ) ) ; for ( int n : nums2 ) { if ( count . containsKey ( n ) ) { intersections . add ( n ) ; count . put ( n , count . get ( n ) - 1 ) ; if ( count . get ( n ) == 0 ) count . remove ( n ) ; } } int [ ] res = new int [ intersections . size ( ) ] ; for ( int i = 0 ; i < intersections . size ( ) ; i ++ ) { res [ i ] = intersections . get ( i ) ; } return res ; }
target:  Use a hash map to record count of each integer in one array. Then go through the other array to find intersection. Count should be updated when an intersection is found.