Q:

How to show maximum same adjacent elements in an array in Java

Im new to Java and i'm doing array sorting problems and i'm having difficulties with this problem. Searched for simmular or duplicate posts, but didn't seem to find any.
The problem is "Find the most same, adjacent elements in an array and write them in to a new array".
My question is. 
How can i store most occurued adjacent elements in an array from another array ?
This is what I have comed up so far.
int[] arr= {2, 1, 1, 3, 3, 2, 2, 2, 2, 1 ,1}; //test array

int counter=0;
int i = 0;
int length = arr.length;
int[] resultArr=new int[length];

for (i=1; i<length; i++)

    {
   if (arr[i-1]==arr[i])
    {
     counter++;      // Counter to represent maximal adjacent occurred elem.
     resultArr[i] = arr[i]  //Writing elements in result array
    }

} 
 System.out.println(counter);  
 System.out.println(resultArr);     

 Input: It is the test array "arr"

 Output 1 : Shoud be 4 representing the maximum of same adjacent elements ( i.e the 2's in the array )
 Output 2 : Must be an array that contains the 4 elements of the array ({2, 2, 2 ,2} )

My output is (directly from console)
1
[I@6d06d69c

A:

Break the problem down, first a method to determine when runs of adjacent values stop;
private static int runsTo(int[] vals, int index) {
    for (int i = index; i < vals.length - 1; i++) {
        if (vals[i] != vals[i + 1]) {
            return i;
        }
    }
    return vals.length;
}

Then we can use that with Arrays.toString(int[])1 as well as Arrays.copyOfRange(int[], int, int) like
int[] arr = { 2, 1, 1, 3, 3, 2, 2, 2, 2, 1, 1 };
int index = 0, count = 0;
for (int i = 0; i < arr.length; i++) {
    int r = runsTo(arr, i);
    int c = r - i + 1;
    if (c > count) {
        index = i;
        count = c;
        i += count - 1;
    }
}
System.out.println(Arrays.toString(arr));
System.out.printf("Starting from %d get %d%n", index, count);
System.out.println(Arrays.toString(Arrays.copyOfRange(arr, index, index + count)));

Which outputs
[2, 1, 1, 3, 3, 2, 2, 2, 2, 1, 1]
Starting from 5 get 4
[2, 2, 2, 2]

1 Java arrays do not override toString(), so you are getting the arrays' hashcode.

