Skip to content

Commit 2a4a34d

Browse files
committed
2215. Find the Difference of Two Arrays: AC
1 parent 2cb4ad0 commit 2a4a34d

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1675,3 +1675,4 @@ mod s2210_count_hills_and_valleys_in_an_array;
16751675
mod s2211_count_collisions_on_a_road;
16761676
mod s2212_maximum_points_in_an_archery_competition;
16771677
mod s2213_longest_substring_of_one_repeating_character;
1678+
mod s2215_find_the_difference_of_two_arrays;
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* [2215] Find the Difference of Two Arrays
3+
*
4+
* Given two 0-indexed integer arrays nums1 and nums2, return a list answer of size 2 where:
5+
*
6+
* answer[0] is a list of all distinct integers in nums1 which are not present in nums2.
7+
* answer[1] is a list of all distinct integers in nums2 which are not present in nums1.
8+
*
9+
* Note that the integers in the lists may be returned in any order.
10+
*
11+
* Example 1:
12+
*
13+
* Input: nums1 = [1,2,3], nums2 = [2,4,6]
14+
* Output: [[1,3],[4,6]]
15+
* Explanation:
16+
* For nums1, nums1[1] = 2 is present at index 0 of nums2, whereas nums1[0] = 1 and nums1[2] = 3 are not present in nums2. Therefore, answer[0] = [1,3].
17+
* For nums2, nums2[0] = 2 is present at index 1 of nums1, whereas nums2[1] = 4 and nums2[2] = 6 are not present in nums1. Therefore, answer[1] = [4,6].
18+
* Example 2:
19+
*
20+
* Input: nums1 = [1,2,3,3], nums2 = [1,1,2,2]
21+
* Output: [[3],[]]
22+
* Explanation:
23+
* For nums1, nums1[2] and nums1[3] are not present in nums2. Since nums1[2] == nums1[3], their value is only included once and answer[0] = [3].
24+
* Every integer in nums2 is present in nums1. Therefore, answer[1] = [].
25+
*
26+
*
27+
* Constraints:
28+
*
29+
* 1 <= nums1.length, nums2.length <= 1000
30+
* -1000 <= nums1[i], nums2[i] <= 1000
31+
*
32+
*/
33+
pub struct Solution {}
34+
35+
// problem: https://leetcode.com/problems/find-the-difference-of-two-arrays/
36+
// discuss: https://leetcode.com/problems/find-the-difference-of-two-arrays/discuss/?currentPage=1&orderBy=most_votes&query=
37+
38+
// submission codes start here
39+
40+
impl Solution {
41+
pub fn find_difference(nums1: Vec<i32>, nums2: Vec<i32>) -> Vec<Vec<i32>> {
42+
let (mut set_1, mut set_2): (
43+
std::collections::HashSet<i32>,
44+
std::collections::HashSet<i32>,
45+
) = (nums1.into_iter().collect(), nums2.into_iter().collect());
46+
47+
vec![
48+
set_1.difference(&set_2).cloned().collect(),
49+
set_2.difference(&set_1).cloned().collect(),
50+
]
51+
}
52+
}
53+
54+
// submission codes end
55+
56+
#[cfg(test)]
57+
mod tests {
58+
use super::*;
59+
60+
#[test]
61+
fn test_2215_example_1() {
62+
let nums1 = vec![1, 2, 3];
63+
let nums2 = vec![2, 4, 6];
64+
65+
let result = vec![vec![1, 3], vec![4, 6]];
66+
67+
assert_eq_sorted!(Solution::find_difference(nums1, nums2), result);
68+
}
69+
70+
#[test]
71+
fn test_2215_example_2() {
72+
let nums1 = vec![1, 2, 3, 3];
73+
let nums2 = vec![1, 1, 2, 2];
74+
75+
let result = vec![vec![3], vec![]];
76+
77+
assert_eq_sorted!(Solution::find_difference(nums1, nums2), result);
78+
}
79+
}

0 commit comments

Comments
 (0)