Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merging together identical values in an array

Tags:

java

arrays

public static String[][][] cleanUp(String[][][] array) {
    for (int f = 0; f < array.length; f++) {
        for (int g = 0; g < array[f].length; g++) {
            int position = 0;
            //boolean flag = false;
            int count = 0;
            for (int h = 0; h < array[f][g].length; h++) {
                if (array[f][g][h].equals(array[f][g][h+1])) count++;
                else {
                    ArrayList<String> temp = new ArrayList<String>(Arrays.asList(array[f][g]));
                    for (int i = count - 1; i > position; i--) {
                        temp.remove(i);
                        position = i-1 ;
                    }
                    temp.set(position, array[f][g][h] + " (" + count + ")");
                }
            }
        }
    }
    return array;
}

Essentially, what I want to do is take a 3D array of strings and have each 1D array in it display quantity for repeated values. For instance, if I had an array of Strings like this:

[go, go, go, go, go, go]
[go, stop, stop, stop]

it would become:

[go (5)]
[go (1), stop (3)]

How could I do this, and what is it I am doing wrong?

like image 544
Calvin Kinzie Avatar asked May 29 '15 06:05

Calvin Kinzie


People also ask

How do you merge data in an array?

To merge elements from one array to another, we must first iterate(loop) through all the array elements. In the loop, we will retrieve each element from an array and insert(using the array push() method) to another array. Now, we can call the merge() function and pass two arrays as the arguments for merging.

Can you concatenate an array?

The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.

Does array concat remove duplicates?

concat() can be used to merge multiple arrays together. But, it does not remove duplicates.


1 Answers

You need to change your last inner loop:

        int count = 0;
        for (int h = 0; h < array[f][g].length; h++) {
            if (array[f][g][h].equals(array[f][g][h+1])) count++;
            //You dont check for out of bound here, so `h + 1` will cause out of bound error
            else {
                ArrayList<String> temp = new ArrayList<String>(Arrays.asList(array[f][g]));
                for (int i = count - 1; i > position; i--) {
                    temp.remove(i);
                    position = i-1 ;
                }
                temp.set(position, array[f][g][h] + " (" + count + ")");
            }
            //Count is not reset after this, so this will be wrong!
        }

How I would do it:

        ArrayList<String> tmp  = new ArrayList<>();
        for (int h = 0; h < array[f][g].length; h++) {
            int count = 1;
            while(h + count < array[f][g].length && array[f][g][h].equals(array[f][g][h+count])) 
               count++;
            tmp.add(array[f][g][h] + "(" + count + ")");
            h += count - 1;//Update h to skip identical element
        }

ArrayList tmp will hold the result for array[f][g], and you should notice how I update h accordingly to skip all identical element.

Update: testing result

like image 160
Pham Trung Avatar answered Sep 29 '22 18:09

Pham Trung