/** 26. Remove Duplicates from Sorted Array https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/ */ class Solution { fun removeDuplicates(nums: IntArray): Int { if (nums==null || nums.isEmpty()){ return 0 } val n = nums.size val tempArray = IntArray(n) var j = 0 for (i in 0 until n-1){ if (nums[i]!=nums[i+1]){ tempArray[j++] = nums[i] } } //add the last element, because it not save in loop for tempArray[j++] = nums[n-1] //modify original array for (i in 0 until j){ nums[i] = tempArray[i] } //return new size of array return j } }