Easy Solution using Set with explanation|| O(n+m)
Intuition\nAs it is mentioned that we need to find distinct integers, frequency of the elements doesnot matter. Hence we can use HashSets over here, to store th
# Intuition\nAs it is mentioned that we need to find distinct integers, frequency of the elements doesnot matter. Hence we can use HashSets over here, to store the distinct elements of each array. To get nums1-nums2 elements, need to store elements of nums1 in hashSet and remove elements which are present in nums2.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nNeed two hashSet for: (1) Integers present in nums1, but not in nums2, (2) Integers present in nums2, but not in nums1. Below steps to be followed:\n1. Iterate over nums1 and Store distinct elements of nums1 in set1.\n2. Iterate over nums2 and remove elements from set1, if exists.\n3. Iterate over nums2 and Store distinct elements of nums2 in set2.\n4. Iterate over nums1 and remove elements from set2, if exists.\nNow, look closely, step 2 and 3, both are iterating in nums2, so we can combine the loop.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n+m)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(n+m)\n\n# Code\nPLEASE UPVOTE IF YOU LIKE.\n\n```\n\nclass Solution {\n    public List<List<Integer>> findDifference(int[] nums1, int[] nums2) {\n       HashSet<Integer> set1 = new HashSet<Integer>();\n       HashSet<Integer> set2 = new HashSet<Integer>();\n       List<List<Integer>> ans=new ArrayList<List<Integer>>();\n\n       //iterate over nums1 and store distinct elements in set1\n       for(int i=0;i<nums1.length;i++)\n       {\n           set1.add(nums1[i]);\n       }\n       \n        //Iterate over nums2 and store distinct elements in set2. \n        //Also, remove matching elements of nums2 from set1\n       for(int i=0;i<nums2.length;i++)\n       {\n           set2.add(nums2[i]);\n           if(set1.contains(nums2[i]))\n                set1.remove(nums2[i]);\n       }\n\n        // remove matching elements of nums1 from set2\n       for(int i=0;i<nums1.length;i++)\n       {\n           if(set2.contains(nums1[i]))\n                set2.remove(nums1[i]);\n       }\n\n        //convert set1 and set2 to answer arrayList\n       ans.add(new ArrayList<Integer>(set1));\n       ans.add(new ArrayList<Integer>(set2));\n       return ans;\n    }\n}\n```
['Java']