Guest User

Untitled

a guest
Jul 18th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. /**
  2. * Follow up for "Remove Duplicates": What if duplicates are allowed at most twice?
  3. *
  4. * Input: 1 1 1 2 2 3
  5. * Output: 1 1 2 2 3
  6. * */
  7. fun removeDuplicates2(array: Array<Int>) : Int {
  8. var j = 0
  9. var repetition = 0
  10. for (i in 1 until array.size) {
  11. if (array[j] != array[i]) {
  12. array[j + 1] = array[i]
  13. j ++
  14. repetition = 0
  15. } else {
  16. if (repetition < 1) {
  17. array[j + 1] = array[i]
  18. j ++
  19. repetition ++
  20. }
  21. }
  22. }
  23. return j + 1
  24. }
Add Comment
Please, Sign In to add comment