1

How to remove two successive elements from array or list?

I've got an array:

new String[]{"FIRST", "SECOND", "SECOND", "THIRD", "FOURTH", "FIRST", "FOURTH"});

I should remove two successive elements from this array.

Elements to remove: "FIRST", "SECOND"

after removing elements new array or list should look like:

new String[]{"SECOND", "THIRD", "FOURTH", "FIRST", "FOURTH"});

now I've got: "SECOND", "FIRST"

result: new String[]{"FOURTH", "FIRST", "FOURTH"})

This is my code:

String[] s1 = new String[arr.length];

String n = "FIRST";
String s = "SECOND";
String w = "THIRD";
String e = "FOURTH";


//I found two successive elements, and element next to them 
//My question is, how can I remove these element from list, 
//or copy array without these elements to a new array?

for (int i = 0; i < arr.length; i++) {
    if (arr[i].equals(n) && arr[i + 1].equals(s)) {
        System.out.println(arr[i + 2]);
    }
}

How to remove two successive elements from array or list?

IMBABOT
  • 55
  • 2
  • 13

3 Answers3

2
public static void main(String[] args) {
    String[] arr = new String[]{"FIRST", "SECOND",
            "SECOND", "THIRD", "FOURTH", "FIRST", "FOURTH"};
    String[] toRemove = new String[]{"FIRST", "SECOND"};
    arr = removeSuccessive(arr, toRemove);
    System.out.println(Arrays.toString(arr));
}

private static String[] removeSuccessive(String[] arr, String[] toRemove) {
    ArrayList<String> res = new ArrayList<>();
    for (int i = 0; i < arr.length; i++) {
        if (!arr[i].equals(toRemove[0]))
            res.add(arr[i]);
        else if (i + 1 < arr.length && !arr[i + 1].equals(toRemove[1]))
            res.add(arr[i]);
        else
            i++;
    }
    return res.toArray(new String[res.size()]);
}

You can check and run this code here: https://ideone.com/v6nPRf

Community
  • 1
  • 1
One Man Crew
  • 9,420
  • 2
  • 42
  • 51
0

You can use regexp for this purpose. Join each array into a single string with some delimiter characters ⦀ and replace all occurrences of the second string in the first string with the same delimiters, then split the string around those delimiters, and get a new array:

String[] arr1 = {"FIRST", "SECOND", "SECOND", "THIRD", "FOURTH",
        "FIRST", "SECOND", "FIRST", "SECOND", "FIRST", "FOURTH"};

String[] arr2 = {"FIRST", "SECOND"};

// join each array into a single
// string with delimiter characters
String str1 = String.join("\u2980", arr1);
String str2 = String.join("\u2980", arr2);

String[] removed = Arrays
        .stream(str1
                // replace all occurrences of the second string
                // in the first string with delimiter characters
                .replaceAll(str2, "\u2980")
                // split a string around delimiter characters
                .split("\u2980"))
        // remove empty strings
        .filter(str -> str.length() > 0)
        .toArray(String[]::new);

System.out.println(Arrays.toString(removed));
// [SECOND, THIRD, FOURTH, FIRST, FOURTH]
-1
package com.sash;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class SashStream {
    public static void main(String[] args) {
        String[] test = new String[]{"FIRST", "SECOND",
                "SECOND", "FIRST", "FOURTH", "FIRST", "FOURTH"};

        String[] out1 = filter(test, "FIRST", "SECOND");
        String[] out2 = filter(out1, "SECOND", "FIRST");

        System.out.println(Arrays.toString(out2));
    }

    private static String[] filter(String[] test, String first, String last) {
        List<String> outputList = new ArrayList<>();
        for (int i = 0; i < test.length; i++) {
            if ((test.length - 1) != i
                    && test[i].equals(first)
                    && test[i + 1].equals(last)) {
                i++;
            } else {
                outputList.add(test[i]);
            }
        }
        return outputList.toArray(new String[0]);
    }
}

Output is:

[FOURTH, FIRST, FOURTH]
Community
  • 1
  • 1
sashwat
  • 607
  • 4
  • 10