0

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

3 Answers3

0

You are doing two mistakes in your code, first you are using the same index i for both your input array arr and result array resultArr, you should use a different one for resultArr like this

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

Second this is not the way to print an Array, either you do it in a for loop like this

for (int i = 0; i < resultArr.length; i++) {
  System.out.println(resultArr[i]);  // will print each in a line
}

or like this

System.out.println(Arrays.toString(resultArr));  // Print all in one line
fujy
  • 5,168
  • 5
  • 31
  • 50
  • Oh i see is my output the adress of the array in the stack memory ? – Veselin Lalov Jul 08 '17 at 15:30
  • 1
    @VeselinLalov it prints a String that describes the identity of the array, in this case the memory location, not the array contents, please check more here https://stackoverflow.com/questions/8410294/why-does-printlnarray-have-strange-output-ljava-lang-string3e25a5?noredirect=1&lq=1 – fujy Jul 08 '17 at 15:37
0

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.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Coincidence, but in the book i found the problem from, suggested doing it in a similar way with using two varriables for the maximal value and where is the current index. Thank you. Works flawlessly. – Veselin Lalov Jul 08 '17 at 16:54
0

Here is your solution :

package com.sujit;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Test {
    public static int count = 0;
    public static void main(String[] args) {
        Integer[] arr= {2, 1, 1, 3, 3, 2, 2, 2, 2, 1 ,1}; //test array
        List<Integer> resultList =Arrays.asList(arr);
        List<Integer> maxOccurence = new ArrayList<Integer>();
        Set<Integer> set = new HashSet<>();

        for (Integer integer : resultList) {
            set.add(integer);
        }
        for (Integer integer : set) {
            int freq = Collections.frequency(resultList, integer);
            maxOccurence.add(freq);
            System.out.println("Element : "+integer+"  Occurences= "+freq);
        }
        System.out.println(Collections.max(maxOccurence));
    }
}
sForSujit
  • 987
  • 1
  • 10
  • 24