Easy Approach | line by line explain | Java
IntuitionThe problem revolves around identifying the intersection between two arrays — essentially finding how many elements from one array appear in the other.
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem revolves around identifying the intersection between two arrays — essentially finding how many elements from one array appear in the other. One straightforward approach would be to check each element of nums1 against every element of nums2, but this would be inefficient with a time complexity of O(n1 * n2). Instead, we can leverage the efficient properties of hash sets.

# Approach
<!-- Describe your approach to solving the problem. -->
This solution aims to find how many elements from nums1 are in nums2 and vice versa, then returns the counts in an integer array. Here's a step-by-step breakdown:

Create Sets for Uniqueness:
Two sets (set1 and set2) are created to store unique elements from nums1 and nums2.

Populate Sets:

Iterate over nums1 and add each element to set1.
Iterate over nums2 and add each element to set2.
Counting Intersections:

Iterate through nums1 and count how many elements exist in set2.
Iterate through nums2 and count how many elements exist in set1.
Return Result:
Return an integer array containing the counts for nums1 and nums2.

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
Building Sets: **O(n1 + n2)**
Counting Intersections: **O(n1 + n2)**

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
Space for Sets: **O(n1 + n2)**

# Code
```java []
class Solution {
    public int[] findIntersectionValues(int[] nums1, int[] nums2) {
        Set<Integer> set1 = new HashSet<>();
        Set<Integer> set2 = new HashSet<>();
        for(int num: nums1)
            set1.add(num);
        for(int num: nums2)
            set2.add(num);
        int count1=0;
        for(int num:nums1){
            if(set2.contains(num))
                count1++;
        }
        int count2=0;
        for(int num:nums2){
            if(set1.contains(num))
                count2++;
        }
        return new int[] {count1,count2};
    }
}
```
['Java']