Java array - eliminate intermediary consecutive numbers

Rex Feral

I want to write an algorithm that pretty much does this with a sorted array full of integers:

1) {8,9,10,14,15,16,19,20,21} -> {8,10,14,16,19,21}

2) {8,9,10,11,12,14,15,16,18,19,20,21} -> {8,12,14,16,18,21}

I've come up with this algorithm:

int l=x.length;    
        for(int k=0;k<l-1;k++)
        {
            if (x[k]+1==x[k+1] && x[k]+2==x[k+2])
            {
                for(int z=k+1;z<l;z++)
                {
                    if(z+1==l)
                        x[z]=0;
                    else
                    x[z]=x[z+1];
                }                
                l--;
            }
        }

This algorithm works well with the above mentioned first example but if I instead put more than 3 consecutive numbers it screws up the result, for example:

{8,9,10,11,12,14,15,16,18,19,20,21} -> {8,10,12,14,16,18,20,21} instead of {8,12,14,16,18,21}

What am I doing wrong? And how can I change my algorithm to successfully eliminate all intermediary consecutive numbers

GPL

@Tunaki is right.

I'd do something along the lines of this:

public static int[] filterImmediary(int[] x) {

    /*
     * Construct a boolean array that will be used to indicate whether the
     * corresponding element in the int array is an immediary consecutive
     * number.
     */
    int l = x.length;
    boolean[] rem = new boolean[l];
    int numKeeps = 0;

    /*
     * Fill boolean array
     */
    for (int k = 0; k < l - 1; k++) {
        int f = 1;
        while (k + f < l && x[k] + f == x[k + f]) {
            if (k + f + 1 < l && x[k] + f + 1 == x[k + f + 1]) {
                rem[k + f] = true;
            }
            f++;
        }
        if (!rem[k]) {
            numKeeps++;
        }
    }

    /*
     * Construct a new array with no immediary consecutive numbers to return
     * instead of mutating the original array.
     */
    int[] newX = new int[numKeeps+1];
    int i = 0;
    for (int k = 0; k < l; k++) {
        if (!rem[k]) {
            newX[i] = x[k];
            i++;
        }
    }
    return newX;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

finding how many consecutive numbers in an array [java]

From Dev

Array into ranges of consecutive numbers

From Dev

Finding Consecutive Numbers in Java

From Dev

Java: Substitute consecutive numbers in a matrix

From Dev

Eliminate consecutive duplicates

From Dev

Practicing Interview Question, Find Largest sum Of Two Consecutive Numbers In An Array Java

From Dev

How do I fill an array with consecutive numbers

From Dev

Find groups of consecutive numbers inside an array

From Dev

Printing only consecutive numbers from an array in matlab

From Dev

R: Count of consecutive numbers over array

From Dev

Finding difference between consecutive numbers in an array in javascript

From Dev

Find if Array Contains 5 consecutive Numbers with Javascript

From Dev

Find number of missing numbers to complete consecutive array

From Dev

Set consecutive equal numbers in an array equal to zero

From Dev

Set consecutive equal numbers in an array equal to zero

From Dev

Printing only consecutive numbers from an array in matlab

From Dev

Find all pairs of consecutive numbers in array

From Dev

Swift array remove consecutive numbers via filter

From Dev

Java String: Checking for consecutive letters or numbers

From Dev

Find largest consecutive numbers in array and output numbers and how many there is

From Dev

Eliminate numbers in string in Python

From Dev

php loop through array to find X consecutive numbers

From Dev

Creating New Matrix which includes consecutive numbers from another array

From Dev

Determining Consecutive Numbers in a 2D Integer array

From Dev

Determining Consecutive Numbers in a 2D Integer array

From Dev

C largest sum of 3 consecutive numbers from array

From Dev

Checking consecutive elements in a string array in java

From Dev

how to eliminate the duplicate numbers from the array, and resize the array to the new number of elements in C++

From Java

Remove space between two or more consecutive numbers in Java?

Related Related

  1. 1

    finding how many consecutive numbers in an array [java]

  2. 2

    Array into ranges of consecutive numbers

  3. 3

    Finding Consecutive Numbers in Java

  4. 4

    Java: Substitute consecutive numbers in a matrix

  5. 5

    Eliminate consecutive duplicates

  6. 6

    Practicing Interview Question, Find Largest sum Of Two Consecutive Numbers In An Array Java

  7. 7

    How do I fill an array with consecutive numbers

  8. 8

    Find groups of consecutive numbers inside an array

  9. 9

    Printing only consecutive numbers from an array in matlab

  10. 10

    R: Count of consecutive numbers over array

  11. 11

    Finding difference between consecutive numbers in an array in javascript

  12. 12

    Find if Array Contains 5 consecutive Numbers with Javascript

  13. 13

    Find number of missing numbers to complete consecutive array

  14. 14

    Set consecutive equal numbers in an array equal to zero

  15. 15

    Set consecutive equal numbers in an array equal to zero

  16. 16

    Printing only consecutive numbers from an array in matlab

  17. 17

    Find all pairs of consecutive numbers in array

  18. 18

    Swift array remove consecutive numbers via filter

  19. 19

    Java String: Checking for consecutive letters or numbers

  20. 20

    Find largest consecutive numbers in array and output numbers and how many there is

  21. 21

    Eliminate numbers in string in Python

  22. 22

    php loop through array to find X consecutive numbers

  23. 23

    Creating New Matrix which includes consecutive numbers from another array

  24. 24

    Determining Consecutive Numbers in a 2D Integer array

  25. 25

    Determining Consecutive Numbers in a 2D Integer array

  26. 26

    C largest sum of 3 consecutive numbers from array

  27. 27

    Checking consecutive elements in a string array in java

  28. 28

    how to eliminate the duplicate numbers from the array, and resize the array to the new number of elements in C++

  29. 29

    Remove space between two or more consecutive numbers in Java?

HotTag

Archive