2248. Intersection of Multiple Arrays
Description
Given a 2D integer array nums where nums[i] is a non-empty array of distinct positive integers, return the list of integers that are present in each array of nums sorted in ascending order.
Example 1:
Input: nums = [[3,1,2,4,5],[1,2,3,4],[3,4,5,6]] Output: [3,4] Explanation: The only integers present in each of nums[0] = [3,1,2,4,5], nums[1] = [1,2,3,4], and nums[2] = [3,4,5,6] are 3 and 4, so we return [3,4].
Example 2:
Input: nums = [[1,2,3],[4,5,6]] Output: [] Explanation: There does not exist any integer present both in nums[0] and nums[1], so we return an empty list [].
Constraints:
1 <= nums.length <= 10001 <= sum(nums[i].length) <= 10001 <= nums[i][j] <= 1000- All the values of
nums[i]are unique.
Solutions
Solution 1: Counting
Traverse the array nums. For each sub-array arr, count the occurrence of each number in arr. Then traverse the count array, count the numbers that appear as many times as the length of the array nums, which are the answers.
The time complexity is $O(N)$, and the space complexity is $O(1000)$. Where $N$ is the total number of numbers in the array nums.
Python3
class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
cnt = [0] * 1001
for arr in nums:
for x in arr:
cnt[x] += 1
return [x for x, v in enumerate(cnt) if v == len(nums)]
Java
class Solution {
public List<Integer> intersection(int[][] nums) {
int[] cnt = new int[1001];
for (var arr : nums) {
for (int x : arr) {
++cnt[x];
}
}
List<Integer> ans = new ArrayList<>();
for (int x = 0; x < 1001; ++x) {
if (cnt[x] == nums.length) {
ans.add(x);
}
}
return ans;
}
}
C++
class Solution {
public:
vector<int> intersection(vector<vector<int>>& nums) {
int cnt[1001]{};
for (auto& arr : nums) {
for (int& x : arr) {
++cnt[x];
}
}
vector<int> ans;
for (int x = 0; x < 1001; ++x) {
if (cnt[x] == nums.size()) {
ans.push_back(x);
}
}
return ans;
}
};
Go
func intersection(nums [][]int) (ans []int) {
cnt := [1001]int{}
for _, arr := range nums {
for _, x := range arr {
cnt[x]++
}
}
for x, v := range cnt {
if v == len(nums) {
ans = append(ans, x)
}
}
return
}
TypeScript
function intersection(nums: number[][]): number[] {
const cnt = new Array(1001).fill(0);
for (const arr of nums) {
for (const x of arr) {
cnt[x]++;
}
}
const ans: number[] = [];
for (let x = 0; x < 1001; x++) {
if (cnt[x] === nums.length) {
ans.push(x);
}
}
return ans;
}
PHP
class Solution {
/**
* @param Integer[][] $nums
* @return Integer[]
*/
function intersection($nums) {
$rs = [];
for ($i = 0; $i < count($nums); $i++) {
for ($j = 0; $j < count($nums[$i]); $j++) {
$hashtable[$nums[$i][$j]] += 1;
if ($hashtable[$nums[$i][$j]] === count($nums)) {
array_push($rs, $nums[$i][$j]);
}
}
}
sort($rs);
return $rs;
}
}
Solution 2
Python3
class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
cnt = Counter()
ans = []
for arr in nums:
for x in arr:
cnt[x] += 1
if cnt[x] == len(nums):
ans.append(x)
ans.sort()
return ans
Java
class Solution {
public List<Integer> intersection(int[][] nums) {
Map<Integer, Integer> cnt = new HashMap<>();
List<Integer> ans = new ArrayList<>();
for (var arr : nums) {
for (int x : arr) {
if (cnt.merge(x, 1, Integer::sum) == nums.length) {
ans.add(x);
}
}
}
Collections.sort(ans);
return ans;
}
}
C++
class Solution {
public:
vector<int> intersection(vector<vector<int>>& nums) {
unordered_map<int, int> cnt;
vector<int> ans;
for (auto& arr : nums) {
for (int& x : arr) {
if (++cnt[x] == nums.size()) {
ans.push_back(x);
}
}
}
sort(ans.begin(), ans.end());
return ans;
}
};
Go
func intersection(nums [][]int) (ans []int) {
cnt := map[int]int{}
for _, arr := range nums {
for _, x := range arr {
cnt[x]++
if cnt[x] == len(nums) {
ans = append(ans, x)
}
}
}
sort.Ints(ans)
return
}
TypeScript
function intersection(nums: number[][]): number[] {
const cnt = new Array(1001).fill(0);
const ans: number[] = [];
for (const arr of nums) {
for (const x of arr) {
if (++cnt[x] == nums.length) {
ans.push(x);
}
}
}
ans.sort((a, b) => a - b);
return ans;
}