# Leetcode – Intersection of Two Arrays Solution in Python

This extensive article will provide an in-depth guide on how to solve the “Intersection of Two Arrays” problem available on LeetCode.

1. Understanding the Problem Statement
2. Exploring Different Approaches
• Brute Force Method
• Using Hashing
• Sorting and Two Pointers
3. Code Implementation for Each Approach
4. Time and Space Complexity Analysis
5. Test Cases and Edge Cases
6. Optimization Techniques
7. Real-World Applications
8. Conclusion

## Understanding the Problem Statement

The “Intersection of Two Arrays” problem on LeetCode asks you to find the intersection of two arrays. That is, you need to find and return the unique elements that are common in both arrays.

Example:

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

Output: [2]

## Exploring Different Approaches

### Brute Force Method

The most naive approach to solve this problem is to use two nested loops. For each element in the first array, check if it is present in the second array. However, this approach is not efficient and should be avoided for large datasets.

### Using Hashing

A more efficient approach is to use a hash set. You can store all elements of the first array in a hash set and then iterate through the second array, checking if the element is in the hash set. This approach is faster as hash sets allow constant-time lookups.

### Sorting and Two Pointers

If the arrays are sorted, you can use the two-pointer technique to find common elements. Maintain two pointers, each for one of the arrays. Move the pointers based on the comparison of elements at the pointer positions.

## Code Implementation for Each Approach

### Brute Force Method

def intersection(nums1, nums2):
result = set()
for num in nums1:
if num in nums2:
return list(result)

### Using Hashing

def intersection(nums1, nums2):
set1 = set(nums1)
return [x for x in set(nums2) if x in set1]

### Sorting and Two Pointers

def intersection(nums1, nums2):
nums1.sort()
nums2.sort()

result = set()
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:
i += 1
j += 1

return list(result)

## Time and Space Complexity Analysis

• Brute Force: Time complexity is O(n * m) and space complexity is O(min(n, m)).
• Using Hashing: Time complexity is O(n + m) and space complexity is O(min(n, m)).
• Sorting and Two Pointers: Time complexity is O(n log n + m log m) and space complexity is O(min(n, m)).

## Test Cases and Edge Cases

Make sure to test your code with various test cases, including edge cases like empty arrays, arrays with one element, and arrays with no common elements.

## Optimization Techniques

Among the three approaches, using hashing is often the most efficient for this problem. However, if the arrays are already sorted or sorting is not a concern, the two-pointer technique is a good option.

## Real-World Applications

Finding the intersection of two datasets is a common task in data analysis and database operations. It is widely used in operations like joining tables, finding common subscribers in marketing lists, or comparing datasets in scientific research.

## Conclusion

The “Intersection of Two Arrays” problem serves as a great example for learning different algorithmic techniques, such as brute force, hashing, and two pointers. Itâ€™s important to understand the trade-offs between these methods in terms of time and space complexity.