A good problem for practicing JAVA 8 new features


  • 0
    B
    public class Solution {
        public int[] intersect(int[] nums1, int[] nums2) {
            ArrayList<Integer> list = new ArrayList<>();
            HashMap<Integer, Integer> map = new HashMap<>();
            for(int i=0; i<nums1.length; i++) map.compute(nums1[i],(k,v)->v==null?1:v+1);
            for(int j=0; j<nums2.length; j++) map.computeIfPresent(nums2[j],(k,v)->{list.add(k); v--; return v==0?null:v;});
            return list.stream().mapToInt(i->i).toArray();
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.