id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

abraskin@mbusd.org > compress
prev  |  next  |  chance

Given an array of numbers it might be possible to compress it by looking for repeating numbers in the sequence.
If there are enough repeating numbers in the sequence then then a new array should be generated where each value is represented by a pair of values instead. The first value is the actual array value and the second is the number of times it repeats.
For example:

data[] = {1,1,1,2,2,3,3}
can be compressed into
data[] = {1,3,2,2,3,2}
which uses one fewer elements.
And the array
data[] = {1,1,1,2,2,3}
can be compressed into
data[] = {1,3,2,2,3,1}
but since it isn't any smaller it should be left alone!
Either return the compressed array if it is smaller than the original but otherwise just return the orginal

compress([1, 1, 1, 2, 2, 3, 3]) → [1, 3, 2, 2, 3, 2]
compress([1, 3, 2, 2, 3, 1]) → [1, 3, 2, 2, 3, 1]
compress([1, 2, 3, 1, 2, 3, 1, 2, 3]) → [1, 2, 3, 1, 2, 3, 1, 2, 3]

...Save, Compile, Run (ctrl-enter)

public int[] compress(int[] data) { }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Difficulty: 299

Copyright Nick Parlante 2017 - privacy