interesection-of-two-arrays.dart (908B)
1 //Return all integers that are in both lists. 2 //To do this first I added the first list to a set. 3 //I then checked the second list to see if the values existed in the set 4 //if they do then they get added to a duplicate set. Finally I converted 5 //the set to a list and returned it. The time complexity of this code is 6 //O(n) where n is the number of integers in both lists combined. 7 //Time: 265ms Beats: 90.91% 8 //Memory: 143MB Beats: 84.85% 9 10 class Solution { 11 List<int> intersection(List<int> nums1, List<int> nums2) { 12 Set<int> set_vals ={}; 13 Set<int> return_vals = {}; 14 for(int i = 0 ; i < nums1.length ; ++i){ 15 set_vals.add(nums1[i]); 16 } 17 for(int i = 0 ; i < nums2.length ; ++i){ 18 if(set_vals.contains(nums2[i])){ 19 return_vals.add(nums2[i]); 20 } 21 } 22 List<int> return_list = return_vals.toList(); 23 return return_list; 24 } 25 } 26 27