We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 8bc0529 commit 8faa8cdCopy full SHA for 8faa8cd
kotlin/2215-find-the-difference-of-two-array.kt
@@ -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
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