I want to define a method named countRepeats that takes in a List of digits 0 to 9 and returns the number of occurrences of adjacent repeated letters.
For example,
Test case 1: the array {0, 1, 2, 2, 1, 2, 2, 1, 3, 3, 1} has three occurrences of repeated digits
Test case 2: the array {0, 1, 1, 1, 1, 2} has one occurrence
Below are my codes:
List<Integer> intlist = new ArrayList<Integer>();
int [] array = new int[]{};
while(sc.hasNext()){
intlist.add(sc.nextInt());
array = intlist.stream().mapToInt(i->i).toArray();
}
System.out.println("Number of occurrences: " + countRepeats(array));
public static long countRepeats(int [] array){
return IntStream.range(0, array.length-1)
.filter(n -> array[n] > 0)
.filter(i -> (array[i] == array[i+1]))
.peek(System.out::println)
.count();
}
However, I failed to get the desired outcome for my test case 2. Anyone can enlighten me ?