class Solution:
    def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
        nums1.sort()
        nums2.sort()
        ans = []
        i, j = 0, 0
        while i < len(nums1) and j < len(nums2):
            if nums1[i] < nums2[j]:
                i += 1
            elif nums1[i] > nums2[j]:
                j += 1
            else:
                if len(ans) == 0 or nums1[i] != ans[-1]:
                    ans.append(nums1[i])
                i += 1
                j += 1
        return ans
class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        int[] dp = new int[1000];
        for(int i:nums1){
            dp[i]++;
        }
        int[] ans = new int[1000];

        //declaring ptr to track ans array index
        int ptr = 0;
        for(int i:nums2){
            if(dp[i] != 0){
                dp[i] = 0;
                ans[ptr] = i;
                ptr++;
            }
        }
        return Arrays.copyOfRange(ans, 0, ptr);
    }
}
Intersection of Two Arrays
Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must be unique and you may return the result in any order.

&nbsp;
Example 1:

Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2]


Example 2:

Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [9,4]
Explanation: [4,9] is also accepted.


&nbsp;
Constraints:


	1 &lt;= nums1.length, nums2.length &lt;= 1000
	0 &lt;= nums1[i], nums2[i] &lt;= 1000


/**
 * @param {number[]} nums1
 * @param {number[]} nums2
 * @return {number[]}
 */
var intersection = function(nums1, nums2) {
    let set = new Set(nums1);
    let set2 = new Set(nums2);
    let result = [];
    for (const val of set) {
        if (set2.has(val)) {
            result.push(val);
        }
    }
    return result;
};
class Solution {
public:
    //what i think is we have to return the intersection elements from both nums
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {

        //result vector which will store those values which will be intersecting in both nums
        vector<int> result;

        //map intersection, which will store values of all the elements in num1
        unordered_map<int,int> intersection;

        //map will store all the values of num1(as key),with corresponding value 1
        for(auto character : nums1)
        {
            intersection[character] = 1;
        }

        //this loop will check if any element of num1 is there in num2? if yes, insert it in result
        //after inserting it once, make its corresponding value to 0
        //so that if you find any elements, that is repeating in num2, as well as present in num1
        //so you dont enter it twice in result, you should enter it once only
        for(auto character : nums2)
        {
            //if found once, it's value would be 1, so entered in result
            //after it is entered in result, its value we change to 0
            //so agin if that same element repeats, due to 0 as its value, we avoid pushing it to result
            if(intersection[character])
            {
                result.push_back(character);
                intersection[character] = 0;
            }
        }

        //after getting all the intersecting elements,we return result
        return result;
    }
};