Guest User

Untitled

a guest
Jul 18th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.48 KB | None | 0 0
  1. /**
  2. * Given a sorted array, remove the duplicates in place such that each element appear only once and
  3. * return the new length.
  4. * Do not allocate extra space for another array, you must do this in place with constant memory.
  5. *
  6. * Input: 1 1 2
  7. * Output: 1 2
  8. * */
  9. fun removeDuplicates(array: Array<Int>) : Int {
  10. var j = 0
  11. for (i in 1 until array.size) {
  12. if (array[j] != array[i]) {
  13. array[j + 1] = array[i]
  14. j ++
  15. }
  16. }
  17. return j + 1
  18. }
Add Comment
Please, Sign In to add comment