Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const array = ["a", "a", "b", "b", "c", "c", "c", "c", "d", "d", "e"]
- function arrayCompress(arr) {
- if(!arr.length) return []
- const result = []
- let current = arr.shift()
- let count = 1
- result.push(current)
- while(arr.length) {
- let next = arr.shift()
- if(current === next) count += 1
- else {
- if(count !== 1) {
- result.push(count)
- result.push(next)
- }
- else {
- result.push(next)
- }
- count = 1
- }
- current = next
- }
- if(count !== 1) result.push(count)
- return result
- }
- arrayCompress(array)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement