LeetCode Entry

2215. Find the Difference of Two Arrays

3.05.2023 easy 2023 kotlin

One way is to use two Sets and just filter them.

2215. Find the Difference of Two Arrays easy


fun findDifference(nums1: IntArray, nums2: IntArray): List<List<Int>> = listOf(
    nums1.subtract(nums2.toSet()).toList(),
    nums2.subtract(nums1.toSet()).toList()
    )

blog post substack

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/201

Intuition

Just do what is asked.

Approach

One way is to use two Sets and just filter them. Another is to use intersect and distinct. Third option is to sort both of them and iterate, that will use \(O(1)\) extra memory, but \(O(nlogn)\) time.

Complexity

  • Time complexity: \(O(n)\)
  • Space complexity: \(O(n)\)