ArrayEncoders { public static int[] runlengthEncode(int[] intArray) { if (intArray.length==0){ return new int[0]; } List<Integer> outList = new ArrayList<>(); int lastInt = intArray[0]; int counter = 1; for (int i=1; i<intArray.length; i++) { if (intArray[i]==lastInt){ counter++; } else{ outList.add(lastInt); outList.add(counter); counter=1; lastInt = intArray[i]; } } outList.add(lastInt); outList.add(counter); return CodecUtils.convertToIntArray(outList); }  static int[] deltaEncode(int[] intArray); static int[] runlengthEncode(int[] intArray);  }
@Test public void runLengthDecodeTest() { int[] inputData = {15,15,15,100,100,111,111,111,111}; int[] outputDataTest = {15,3,100,2,111,4}; int[] outputData = ArrayEncoders.runlengthEncode(inputData); assertArrayEquals(outputDataTest, outputData); }
@Test public void emptyRunLengthDecodeTest() { int[] inputData = {}; int[] outputDataTest = {}; int[] outputData = ArrayEncoders.runlengthEncode(inputData); assertArrayEquals(outputDataTest, outputData); }