Intersection of Two Arrays
Intersection of Two Arrays is a Easy Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Two Pointers pattern, in the In-place Array Modification subpattern.
Problem statement
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.
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.
Constraints:
- 1 <= nums1.length, nums2.length <= 1000
- 0 <= nums1[i], nums2[i] <= 1000
Topics and companies
Topics: Array, Hash Table, Two Pointers, Binary Search, Sorting.
Reported in interviews at Facebook, Amazon, Bloomberg, ByteDance, Apple.
How to practise Intersection of Two Arrays on Thita.ai
Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(m + n) time and O(min(m, n)) space.
Open Intersection of Two Arrays in the code editor, or read the Intersection of Two Arrays editorial for a worked solution with its approach and complexity analysis.
Where Intersection of Two Arrays sits in the DSA pattern sheet
- Two Pointers pattern guide
- In-place Array Modification subpattern
- The full DSA patterns sheet
- All coding practice problems
Problems related to Intersection of Two Arrays
Other problems that use the same In-place Array Modification and Two Pointers techniques:
- Move Pieces to Obtain a String — Medium, same subpattern
- Move Zeroes — Easy, same subpattern
- Remove Duplicates from Sorted Array — Easy, same subpattern
- Remove Duplicates from Sorted Array II — Medium, same subpattern
- Remove Element — Easy, same subpattern
- Separate Black and White Balls — Medium, same subpattern
Preparing your workspace
Progress is saved as you study.