Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Given a sorted array, remove the duplicates in place such that each element appear only once and
- * return the new length.
- * Do not allocate extra space for another array, you must do this in place with constant memory.
- *
- * Input: 1 1 2
- * Output: 1 2
- * */
- fun removeDuplicates(array: Array<Int>) : Int {
- var j = 0
- for (i in 1 until array.size) {
- if (array[j] != array[i]) {
- array[j + 1] = array[i]
- j ++
- }
- }
- return j + 1
- }
Add Comment
Please, Sign In to add comment