简体   繁体   中英

Delete element last occurrence in array

If a row of an array had the characters:

line = "I appreciate you helping me out!"

Let's say I wanted to delete the last occurrence of the letter 'e'. How? ie the result should be:

"I appreciate you helping m out!"

Here is my idea and I know the syntax is wrong. I start at 26 because that's the last time position an 'e' happens in the length of the string.

for (int i = 26; i < line.length() ; i++)
    line.chars('e') = (line.chars('') && line.chars(i));
}
String line = "I appreciate you helping me out!";
int index = line.lastIndexOf('e');
if(index != -1) //prevent IndexOutOfBoundsException in case it can't find the char
    line = new StringBuilder(line).deleteCharAt(index).toString();

or

String line = "I appreciate you helping me out!";
for (int i = line.length(); --i >= 0;){
    if(line.charAt(i) == 'e'){
        line = line.substring(0, i) + line.substring(i + 1);
        break;
    }
}

Or you can use regular expression like below :

String word = "I appreciate you helping me out!";
System.out.println(word.replaceAll("[e]([^e]*)$","$1"));

Out-put :

I appreciate you helping m out!

please try the below code snippet. Ideally it should work in generic way.

    StringBuilder line = new StringBuilder(
            "I appreciate you helping me out!");

    System.out.println("Given input String :" + line);
    int lastOccuranceIndex = 0;
    int deleteIndex = 0;
    char[] charr = line.toString().toLowerCase().toCharArray();
    Map<Character, List<Integer>> charMap = new LinkedHashMap<Character, List<Integer>>();
    List<Integer> indexList = null;
    for (int i = 0; i < charr.length; i++) {
        if (charMap.containsKey(charr[i])) {
            indexList = charMap.get(charr[i]);
            indexList.add(i);
            charMap.put(charr[i], indexList);
        } else if (Character.isAlphabetic(charr[i])) {
            indexList = new ArrayList<Integer>();
            indexList.add(i);
            charMap.put(charr[i], indexList);
        }
    }
    for (Entry<Character, List<Integer>> entry : charMap.entrySet()) {
        indexList = entry.getValue();
        if (indexList.size() > 2) {
            // System.out.println(entry.getKey()
            // +" last but one : "+indexList.get(indexList.size() -2));
            if (indexList.get(indexList.size() - 2) > lastOccuranceIndex) {
                lastOccuranceIndex = indexList.get(indexList.size() - 2);
                deleteIndex = indexList.get(indexList.size() - 1);
            }
        }

    }
    System.out.println("last occurance character index  "
            + lastOccuranceIndex + " and the character to delete is :"
            + charr[lastOccuranceIndex]);
    char deleteChar = line.charAt(deleteIndex);
    System.out.println("deleteChar :" + deleteChar + " at index :"
            + deleteIndex);
    line = line.deleteCharAt(deleteIndex);
    System.out.println("String content after delete operation : " + line);

output:

Given input String :I appreciate you helping me out!

last occurance character index 18 and the character to delete is :e

deleteChar :e at index :26

String content after delete operation : I appreciate you helping m out!

Regex to the rescue:

line = line.replaceAll("e(?=[^e]*$)", "");

The regex (?=[^e]*$) is a look ahead the requires there to be no occurrences of e anywhere after the e being matched.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM