BEATS 100% USER || EASY|| BEGINNER
Intuition\n Describe your first thoughts on how to solve this problem. \n\n\nThis approach efficiently counts the occurrences of elements in both arrays and the
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n![image.png](https://assets.leetcode.com/users/images/f3570731-114d-4b90-a3b7-310fe78e0321_1702138543.933777.png)\n\nThis approach efficiently counts the occurrences of elements in both arrays and then calculates the number of unique elements from each array that are present in the other. The result is returned as an array [count2, count1], representing the counts as described in the problem statement.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nFrequency Count:\n\nInitialize two arrays (num1freq and num2freq) to store the frequency of elements. The size of these arrays is set to 102 based on the given constraints.\nFor each element in nums1, increment the corresponding frequency in num1freq.\nIntersection Count:\n\nInitialize count1 to 0. For each element in nums2, if its frequency in num1freq is not zero, increment count1. This gives the count of unique elements from nums2 present in nums1.\n\nInitialize count2 to 0. For each element in nums1, if its frequency in num2freq is not zero, increment count2. This gives the count of unique elements from nums1 present in nums2.\n\nResult Array:\n\nCreate an array ans of size 2.\nSet ans[0] to count2 and ans[1] to count1.\nReturn Result:\n\nReturn the ans array as the final result.\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n![image.png](https://assets.leetcode.com/users/images/8e3c5b62-a50c-4a65-b484-87252cb77c7f_1702138458.1984313.png)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public int[] findIntersectionValues(int[] nums1, int[] nums2) {\n        // Initialize frequency arrays for nums1 and nums2\n        int[] num1freq = new int[102];\n        int[] num2freq = new int[102];\n\n        // Initialize counters for intersection counts\n        int count1 = 0; // Number of unique elements from nums2 present in nums1\n        int count2 = 0; // Number of unique elements from nums1 present in nums2\n\n        // Count frequencies of elements in nums1\n        for (int i = 0; i < nums1.length; i++) {\n            num1freq[nums1[i]]++;\n        }\n\n        // Count intersection elements in nums2\n        for (int i = 0; i < nums2.length; i++) {\n            if (num1freq[nums2[i]] != 0) {\n                count1++;\n            }\n        }\n\n        // Count frequencies of elements in nums2\n        for (int i = 0; i < nums2.length; i++) {\n            num2freq[nums2[i]]++;\n        }\n\n        // Count intersection elements in nums1\n        for (int i = 0; i < nums1.length; i++) {\n            if (num2freq[nums1[i]] != 0) {\n                count2++;\n            }\n        }\n\n        // Create result array\n        int[] ans = new int[2];\n        ans[0] = count2; // Number of unique elements from nums1 present in nums2\n        ans[1] = count1; // Number of unique elements from nums2 present in nums1\n\n        return ans;\n    }\n}\n\n```\n![image.png](https://assets.leetcode.com/users/images/31c4ae29-e46f-43f5-8d94-e2ece9b5c532_1702138452.62848.png)\n
['Java']