Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Follow up for "Remove Duplicates": What if duplicates are allowed at most twice?
- *
- * Input: 1 1 1 2 2 3
- * Output: 1 1 2 2 3
- * */
- fun removeDuplicates2(array: Array<Int>) : Int {
- var j = 0
- var repetition = 0
- for (i in 1 until array.size) {
- if (array[j] != array[i]) {
- array[j + 1] = array[i]
- j ++
- repetition = 0
- } else {
- if (repetition < 1) {
- array[j + 1] = array[i]
- j ++
- repetition ++
- }
- }
- }
- return j + 1
- }
Add Comment
Please, Sign In to add comment