first-missing-positive.dart (731B)
1 //Given a list nums return the lowest positive integer that does not appear 2 //in the nums list. 3 //To solve this I first created a set and placed all values of the nums list into it. 4 //Then I went through all positive numbers until finding the first one that does not appear in the set. 5 //At that point I return the value that was not found. 6 //Time: 295ms Beats: 100% 7 //Memory: 178MB Beats: 50% 8 9 class Solution { 10 int firstMissingPositive(List<int> nums) { 11 Set<int> vals = {}; 12 for(int i = 0 ; i < nums.length ; ++i){ 13 vals.add(nums[i]); 14 } 15 int itr = 1; 16 while(true){ 17 if(!vals.contains(itr)){ 18 return itr; 19 } 20 itr += 1; 21 } 22 return 0; 23 } 24 }