remove-duplicates.dart (1025B)
1 //Given an input list remove duplicate values. 2 //Leetcode was dumb on this one and had to use some stupid side effect crap 3 //what I did was get all unique values in the list by creating a set which 4 //innately only stores unique values then switching that set back to a list. 5 //I would not have needed this much code if they let me use the .toList method 6 //for the set. 7 8 //Runtime: 262ms Beats: 100% 9 //Memory: 145.3MB Beats: 45.26% 10 11 void main(){ 12 Solution sol = new Solution(); 13 List<int> nums = [0 , 3, 4, 5, 6,6,6,7,8,9,9,6,5,3,2]; 14 //Init list 15 print(nums); 16 //Shorten list 17 sol.removeDuplicates(nums); 18 //New list 19 print(sol.num_list); 20 } 21 22 class Solution { 23 List<int> num_list = []; 24 25 int removeDuplicates(List<int> nums) { 26 Set<int> nums_set = nums.toSet(); 27 int count = 0; 28 for(var i in nums_set){ 29 nums[count] = i; 30 count += 1; 31 } 32 for(int i = 0; i < nums_set.length; ++i){ 33 num_list.add(nums[i]); 34 } 35 return nums_set.length; 36 } 37 }