1

I am a bit stuck so if anyone has a spare moment it would be a great help for me. I am using eclipse and the program is compiling and running. but there is a runtime error. in the array {2, 1, 1, 2, 3, 3, 2, 2, 2, 1} I want to print {2, 2, 2} which is the numbers with the highest repeating times in the array. What I am getting is: 0 1 0 0 3 0 2 2 0

Thank you guys and here is my code.

public class SameIndexInArray
{
    public static void main(String[] args) 
    {
        int[] array = {2, 1, 1, 2, 3, 3, 2, 2, 2, 1};
        int[] subarray = new int[array.length]; //store the values should be {2, 2, 2}

        int max = 1;
        int total = 1;

        for(int i=0; i<array.length-1; i++)
        {
            if(array[i] != array[i + 1])
            {
                max = 1;
            }
            else if(array[i] == array[i + 1])
            {
                max++;
                total = max;
                subarray[i] = array[i]; // here is the issue
            }
            System.out.println(subarray[i]);
        }
        //System.out.println(total);
    }
}
4
  • ignore that, i see what you're trying to do now :) Commented Feb 28, 2012 at 10:02
  • no it should be only {2,2,2} because they are in a row one after the other Commented Feb 28, 2012 at 10:03
  • you need to take care of i, and max. The logic is flawed. Commented Feb 28, 2012 at 10:05
  • Thank you all for your time I appreciate it! Commented Feb 28, 2012 at 10:09

6 Answers 6

2

You only store facultatively into subarray, so you should define a separate counter (let's say j) for subarray index counting, and say subarray[j++] = array[i]. And, you shouldn't output subarray for each index of array, so move that println into the second if clause.

Sign up to request clarification or add additional context in comments.

1 Comment

Alternatively you could also do a sort such as a bubble sort roseindia.net/java/beginners/arrayexamples/bubbleSort.shtml and then print from array.length backwards while the elements are the same.
2

see if this works

    int[] array = {2, 1, 1, 2, 3, 3, 2, 2, 2, 1};

    int frequency = 0;
    int num = 0;

    for(int i=0; i<array.length-1; i++)
    {
        int lfreq = 1;
        int lnum = array[i];
        while(array[i] == array[i+1]){
            lfreq++;
            i++;
        }
        if(lfreq >= frequency){
            frequency = lfreq;
            num = lnum;
        }

    }
    int[] subarray = new int[frequency];
    for(int i=0; i < frequency; i++)
        subarray[i] = num;

    System.out.println(Arrays.toString(subarray));

Comments

1

You need to use another index but "i" you can't relate to 2 arrays with the same index

Comments

1

Your problem is that all values in subarray are initialized with 0 and you only edit the values when there is an actual sequence, starting with the second element.

The whole subarray is unneccessary. Just save the start index and the length of the subquery ;)

What I mean is something like this:

int[] array = {2, 1, 1, 2, 3, 3, 2, 2, 2, 1};

int startIndex = 0;
int length = 0;

int longestIndex = 0;
int longestLength = 0;

for(int i=0; i<array.length-1; i++)
{
    if(array[i] != array[i + 1])
    {
        if (length > longestLength) {
            longestLength = length;
            longestIndex = startIndex;
        }
        startIndex = i;
        length = 1;
    }
    else if(array[i] == array[i + 1])
    {
        length++;
    }
}

if (length > longestLength) {
    longestLength = length;
    longestIndex = startIndex;
}

Now you that you know where your longest sequence starts and how long it is you can build your new array:

int[] sequence = new int[longestLength];
for (int i = 0; i < longestLength; i++) {
    sequence[i] = array[i + startIndex];
}

Comments

1

Thats because you are inserting at an index position "i" into subarray. For example, The second time the loop runs.

array[1] == array[2] is true and

subarray[i] = array[i]; 

runs. So at this moment the contents of subarray is {0,1,0,0,0,0,0,0,0}. Note that arrays are initialized to 0 by default.

This is how you could do it.

int[] array = {2, 1, 1, 2, 3, 3, 2, 2, 2, 1};
         //store the values should be {2, 2, 2}

        int max = 1;
        int total = 1;
        int value = 0;
        for(int i=0; i<array.length-1; i++)
        {
            if(array[i] != array[i + 1])
            {
                max = 1;
            }
            else if(array[i] == array[i + 1])
            {
                max++;
                total = max;
                value = array[i];
            }

        }
        int[] subarray = new int[total];
        for(int i=0; i<total; i++)
            subarray[i] = value;

Comments

0

public static void main(String[] args) {

 int[] ar = { 2, 1, 1, 2, 3, 3, 2, 2, 2, 1 };
    int max=0,
        maxStart=0,
        count=1;

    for(int i=1; i<ar.length; i++) {
        if (ar[i-1] == ar[i]) {
            count++;
            if(count > max) {
                max = count;
                maxStart = i-count+1;
            }
        }else {
            count=1;
        }
    }

    System.out.println("Sub array begins from " + maxStart);

    for(int i = maxStart; i < maxStart + max; i++) {
        System.out.print(ar[i] + " ");
    }


}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.