commit 042885677c6be16c19b999dbe38da52ba182e253 parent 6ef5b6f9f1f65991b90ee770efb0cf356998821d Author: AndrewLockVI <andrewlaack1@gmail.com> Date: Wed, 3 May 2023 08:44:48 -0500 Completed find the difference problem using a set and dart Diffstat:
| A | find-the-difference-of-two-array/difference-of-two-arraysV2.dart | | | 25 | +++++++++++++++++++++++++ |
1 file changed, 25 insertions(+), 0 deletions(-)
diff --git a/find-the-difference-of-two-array/difference-of-two-arraysV2.dart b/find-the-difference-of-two-array/difference-of-two-arraysV2.dart @@ -0,0 +1,25 @@ +//Same as other solution but better because it uses +//sets instead of a map which decreases overhead memory usage. +//Time: 332ms Beats: 80% +//Memory: 151MB Beats: 50% + +class Solution { + List<List<int>> findDifference(List<int> nums1, List<int> nums2) { + Set<int> set1 = {}; + Set <int> set2 = {}; + for(int i = 0 ; i < nums1.length ; ++i){ + set1.add(nums1[i]); + } + for(int i = 0 ; i < nums2.length ; ++i){ + set2.add(nums2[i]); + } + for(int i = 0 ; i < nums1.length ; ++i){ + set2.remove(nums1[i]); + } + for(int i = 0 ; i < nums2.length ; ++i){ + set1.remove(nums2[i]); + } + + return [set1.toList() , set2.toList()]; + } +}