Skip to content

Commit 8faa8cd

Browse files
authored
Create 2215-find-the-difference-of-two-array.kt
1 parent 8bc0529 commit 8faa8cd

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

Diff for: kotlin/2215-find-the-difference-of-two-array.kt

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Short answer
3+
*/
4+
class Solution {
5+
fun findDifference(nums1: IntArray, nums2: IntArray) = listOf(
6+
(nums1.toSet() subtract nums2.toSet()).toList(),
7+
(nums2.toSet() subtract nums1.toSet()).toList()
8+
)
9+
}
10+
11+
/*
12+
* Or if you are interested in the "logic"
13+
*/
14+
class Solution {
15+
fun findDifference(nums1: IntArray, nums2: IntArray): List<List<Int>> {
16+
val nums1Set = nums1.toSet()
17+
val nums2Set = nums2.toSet()
18+
val res1 = HashSet<Int>()
19+
val res2 = HashSet<Int>()
20+
21+
for (n in nums1) {
22+
if (n !in nums2Set)
23+
res1.add(n)
24+
}
25+
26+
for (n in nums2) {
27+
if (n !in nums1Set)
28+
res2.add(n)
29+
}
30+
31+
return listOf(
32+
res1.toList(),
33+
res2.toList()
34+
)
35+
}
36+
}

0 commit comments

Comments
 (0)