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