# How to Sum Consecutives in Java

## The challenge

You are given a list/array which contains only integers (positive and negative). Your job is to sum only the numbers that are the same and consecutive. The result should be one list.

Extra credit if you solve it in one line. You can assume there is never an empty list/array and there will always be an integer.

Same meaning: 1 == 1

1 != -1

Examples:

```.wp-block-code{border:0;padding:0}.wp-block-code>div{overflow:auto}.shcb-language{border:0;clip:rect(1px,1px,1px,1px);-webkit-clip-path:inset(50%);clip-path:inset(50%);height:1px;margin:-1px;overflow:hidden;padding:0;position:absolute;width:1px;word-wrap:normal;word-break:normal}.hljs{box-sizing:border-box}.hljs.shcb-code-table{display:table;width:100%}.hljs.shcb-code-table>.shcb-loc{color:inherit;display:table-row;width:100%}.hljs.shcb-code-table .shcb-loc>span{display:table-cell}.wp-block-code code.hljs:not(.shcb-wrap-lines){white-space:pre}.wp-block-code code.hljs.shcb-wrap-lines{white-space:pre-wrap}.hljs.shcb-line-numbers{border-spacing:0;counter-reset:line}.hljs.shcb-line-numbers>.shcb-loc{counter-increment:line}.hljs.shcb-line-numbers .shcb-loc>span{padding-left:.75em}.hljs.shcb-line-numbers .shcb-loc::before{border-right:1px solid #ddd;content:counter(line);display:table-cell;padding:0 .75em;text-align:right;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;white-space:nowrap;width:1%}```[1,4,4,4,0,4,3,3,1] # should return [1,12,0,4,6,1]

"""So as you can see sum of consecutives 1 is 1
sum of 3 consecutives 4 is 12
sum of 0... and sum of 2
consecutives 3 is 6 ..."""

[1,1,7,7,3] # should return [2,14,3]
[-5,-5,7,7,12,0] # should return [-10,14,12,0]```Code language: PHP (php)```

## The solution in Java code

Option 1:

``````import java.util.*;
public class Consecutives {
public static List<Integer> sumConsecutives(List<Integer> s) {
int previous = Integer.MAX_VALUE;
for (Integer v: s){
l.add(v == previous? l.pollLast() + v : v);
previous = v;
}
return l;
}
}
```Code language: Java (java)```

Option 2:

``````import java.util.ArrayList;
import java.util.List;
public class Consecutives {
public static List<Integer> sumConsecutives(List<Integer> s) {
List<Integer> accumulator = new ArrayList<>();
for (int i = 0, sum = 0; i < s.size(); i++) {
sum += s.get(i);
if (i == s.size() - 1 || s.get(i) != s.get(i + 1)) {
sum = 0;
}
}
return accumulator;
}
}
```Code language: Java (java)```

Option 3:

``````import java.util.*;
public class Consecutives {
public static List<Integer> sumConsecutives(List<Integer> list) {
List<Integer> result = new ArrayList<>();
int sum = list.get(0);
int i = 0;
while (i < list.size()){
if (i == list.size() - 1) {
} else if (list.get(i).equals(list.get(i + 1))) {
sum += list.get(i);
} else {
sum = list.get(i + 1);
}
i++;
}
return result;
}
}
```Code language: Java (java)```

## Test cases to validate our solution

``````import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;

public class ConsecutivesTest {

@Test
public void test() {
System.out.println("Basic Tests");
List<Integer> i = Arrays.asList(1,4,4,4,0,4,3,3,1);
List<Integer> o = Arrays.asList(1,12,0,4,6,1);
System.out.println("Input: {1,4,4,4,0,4,3,3,1}");
assertEquals(o, Consecutives.sumConsecutives(i));
i = Arrays.asList(-5,-5,7,7,12,0);
o = Arrays.asList(-10,14,12,0);
System.out.println("Input: {-5,-5,7,7,12,0}");
assertEquals(o, Consecutives.sumConsecutives(i));
i = Arrays.asList(1,1,7,7,3);
o = Arrays.asList(2,14,3);
System.out.println("Input: {1,1,7,7,3}");
assertEquals(o, Consecutives.sumConsecutives(i));
i = Arrays.asList(3,3,3,3,1);
o = Arrays.asList(12, 1);
System.out.println("Input: {3,3,3,3,1}");
assertEquals(o, Consecutives.sumConsecutives(i));
i = Arrays.asList(2,2,-4,4,5,5,6,6,6,6,6,1);
o = Arrays.asList(4, -4, 4, 10, 30, 1);
System.out.println("Input: {2,2,-4,4,5,5,6,6,6,6,6,1}");
assertEquals(o, Consecutives.sumConsecutives(i));
i = Arrays.asList(1,1,1,1,1,3);
o = Arrays.asList(5, 3);
System.out.println("Input: {1,1,1,1,1,3}");
assertEquals(o, Consecutives.sumConsecutives(i));
i = Arrays.asList(1,-1,-2,2,3,-3,4,-4);
o = Arrays.asList(1, -1, -2, 2, 3, -3, 4, -4);
System.out.println("Input: {1,-1,-2,2,3,-3,4,-4}");
assertEquals(o, Consecutives.sumConsecutives(i));

}

}
```Code language: Java (java)```
Tags:
Subscribe
Notify of