349. Intersection of Two Arrays

Intersection of Two Arrays (LeetCode 349) 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.

How to solve Intersection of Two Arrays

Intersection of Two Arrays is a In-Place Array Modification problem, part of Two Pointers. Learn in-place array modification using two pointers. Remove duplicates, move zeroes, and sort colors efficiently.

Work towards the solution in these steps:

  1. Consider the properties of sets and how they can help identify unique common elements between two arrays.
  2. Sorting both arrays can simplify the process of finding common elements by allowing a linear scan.
  3. Think about using two pointers starting at the beginning of each sorted array to traverse and compare elements efficiently.

The reference solution runs in O(m + n) time and O(min(m, n)) space.

The Intersection of Two Arrays solution and editorial walks through the full approach with reference code.

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

Problems related to Intersection of Two Arrays

Other problems that use the same In-place Array Modification and Two Pointers techniques:

Intersection of Two Arrays FAQ

What is the LeetCode number of Intersection of Two Arrays?

Intersection of Two Arrays is LeetCode problem 349, rated Easy.

What pattern is used to solve Intersection of Two Arrays?

Intersection of Two Arrays is a In-Place Array Modification problem. It belongs to the Two Pointers pattern, in the In-place Array Modification subpattern.

What is the time complexity of Intersection of Two Arrays?

The reference solution runs in O(m + n) time and O(min(m, n)) space.

Which companies ask Intersection of Two Arrays?

Intersection of Two Arrays has been reported in interviews at Facebook, Amazon, Bloomberg, ByteDance, Apple.

Preparing this workspace.