class Weights {

    public static double[] buildRleWeights(List<Number> weightList, int limit) {
        double[] weights = new double[weightList.size() + (weightList.size() / (limit * 2))];

        double latestValue = Double.POSITIVE_INFINITY;
        int counter = 0;

        int i = 0;
        for (Number value : weightList) {
            if (value.doubleValue() == latestValue || (Double.isNaN(latestValue) && Double.isNaN(value.doubleValue()))) {
                counter++;
            } else {
                if (counter > limit) {
                    weights[i++] = Double.POSITIVE_INFINITY;
                    weights[i++] = counter;
                    weights[i++] = latestValue;
                    counter = 1;
                } else {
                    if (counter > 0) {
                        for (int j = 0; j < counter; j++) {
                            weights[i++] = latestValue;
                        }
                    }
                    counter = 1;
                }
                latestValue = value.doubleValue();
            }
        }

        if (counter > limit) {
            weights[i++] = Double.POSITIVE_INFINITY;
            weights[i++] = counter;
            weights[i++] = latestValue;
        } else {
            for (int j = 0; j < counter; j++) {
                weights[i++] = latestValue;
            }
        }

        return Arrays.copyOf(weights, i);
    }

}

class WeightsTest {

    @Test
    public void rleWithMoreThanOneRepeatedValue() throws Exception {

        Number[] values = {2.0, 2.0, 4.0, 4.0, 6.0, 6.0};
        List<Number> weightList = Arrays.asList(values);
        double[] actuals = Weights.buildRleWeights(weightList, 1);
        System.out.println("actuals = " + Arrays.toString(actuals));
        assertArrayEquals(new double[]{
                Double.POSITIVE_INFINITY, 2.0, 2.0,
                Double.POSITIVE_INFINITY, 2.0, 4.0,
                Double.POSITIVE_INFINITY, 2.0, 6.0}, actuals, 0.01);
    }

}