100% beat | 2 Solutions | Array |  Set |
\n# Time Complexity\n Both of solutions are:   O(n) \n\n# Code\n\n## 1- Array Solution\n\n100% Beats\n\n\nclass Solution {\n    public List<List<Integer>> findD
\n# Time Complexity\n Both of solutions are:   $$O(n)$$ \n\n# Code\n\n## 1- Array Solution\n\n100% Beats\n\n```\nclass Solution {\n    public List<List<Integer>> findDifference(int[] nums1, int[] nums2) {\n        boolean[] n1 = new boolean[2001];\n        boolean[] n2 = new boolean[2001];\n\n        ArrayList<Integer> dif1 = new ArrayList<>();\n        ArrayList<Integer> dif2 = new ArrayList<>();\n\n        for (int num : nums1) {\n            n1[num + 1000] = true;\n        }\n\n        for (int num : nums2) {\n            n2[num + 1000] = true;\n\n            if (!n1[num + 1000]) {\n                n1[num + 1000] = true;  // to avoid duplications\n                dif2.add(num);\n            }\n        }\n\n        for (int num : nums1) {\n            if (!n2[num + 1000]) {\n                n2[num + 1000] = true;  // to avoid duplications\n                dif1.add(num);\n            }\n        }\n\n        return List.of(dif1, dif2);\n    }\n}\n```\n\n## 2- Set Solution\n\n\n~68% Beats\n\n```\nclass Solution {\n       public List<List<Integer>> findDifference(int[] nums1, int[] nums2){\n        HashSet<Integer> set1 = new HashSet<>();\n        HashSet<Integer> set2 = new HashSet<>();\n        ArrayList<Integer> diff1 = new ArrayList<>();\n        ArrayList<Integer> diff2 = new ArrayList<>();\n\n\n        for (int i : nums1) {\n            set1.add(i);\n        }\n\n        for (int i : nums2) {\n            set2.add(i);\n            if (set1.add(i)) {\n                diff2.add(i);\n            }\n        }\n\n        for (int i : nums1) {\n            if (set2.add(i)) {\n                diff1.add(i);\n            }\n        }\n\n        return List.of(diff1, diff2);\n    }\n}\n```\n\n<br/>\n\n \n\n<img src="https://assets.leetcode.com/users/images/085853ae-9586-479f-acf2-ec3ce30fd0e7_1703932262.0024223.png" style="width:400px;height:auto;">\n\n\n<br/>\n<br/>\n \n
['Java']