Merge 2 Sorted Array xx Two Pointer
Intuition\n Describe your first thoughts on how to solve this problem. \nI have been solving Union and Intersection problems by merging two sorted array using t
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nI have been solving Union and Intersection problems by merging two sorted array using two-pointer (LC349, LC350). To check for duplicates use the logic used in LC26, LC80.\n\n# Complexity\n- Time complexity: O(n log n + m log m)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nBecause sorting is involved.\n- Space complexity: O(n + m) \n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public List<List<Integer>> findDifference(int[] nums1, int[] nums2) {\n        Arrays.sort(nums1);\n        Arrays.sort(nums2);\n        // ArrayList<ArrayList<Integer>> arr = new ArrayList<>();\n        List<List<Integer>> arr = new ArrayList<>();\n        for(int i = 0; i < 2; i++){\n            arr.add(new ArrayList<Integer>());\n        }\n        int n = nums1.length; int m = nums2.length;\n        int i = 0; int j = 0;\n        int x = 0; int y = 0;\n        while(i < n && j < m) {\n            if(nums1[i] < nums2[j]){\n                if(x < 1 || nums1[i] != arr.get(0).get(x-1)){\n                    arr.get(0).add(nums1[i]);\n                    x++;\n                }\n                i++;\n            } else if (nums1[i] > nums2[j]){\n                j++;\n            } else {\n                i++;\n                // j++;\n            }\n        }\n        while(i < n){\n            // System.out.println("avy" + nums1[i]);\n            if(x < 1 || nums1[i] != arr.get(0).get(x-1)){\n                arr.get(0).add(nums1[i]);\n                x++;\n            }\n            i++;\n        }\n\n        i = 0; j = 0;\n        while(i < n && j < m){\n            if(nums2[j] < nums1[i]){\n                if(y < 1 || nums2[j] != arr.get(1).get(y-1)){\n                    arr.get(1).add(nums2[j]);\n                    y++;\n                }\n                j++;\n            } else if (nums2[j] > nums1[i]){\n                i++;\n            } else {\n                // i++;\n                j++;\n            }\n        }\n\n        while(j < m){\n            if(y < 1 || nums2[j] != arr.get(1).get(y-1)){\n                arr.get(1).add(nums2[j]);\n                y++;\n            }\n            j++;\n        }\n\n        return arr;\n    }\n}\n\n```
['Two Pointers', 'Sorting', 'Java']