For an assignment, I have to create a method that takes in an ArrayList and removes repeated elements from it (case insensitive). Additionally, it needs to change the case of the element to match that of the last occurrence of that String in the ArrayList.
Right now I'm trying a 3-step process: creating a new ArrayList of Strings and filling it with elements identical to those of the input one. Then I use nested for-loops to iterate through it, change repeats of elements to "REMOVE_THIS_STRING", and change the first instance of each String to match the capitalization of the last instance. Then afterwards, in another loop, I go through and remove all elements that match the "REMOVE_THIS_STRING" String. I know that this isn't the most efficient way to go about things, but I haven't worked much with other types of collections so I'm hesitant to mess with those right now and would prefer an approach that only uses ArrayLists, if possible.
/*
Code that creates the NewArrayList ArrayList and fills it
with elements identical to that of the input ArrayList
*/
for(int i=0; i<input.size(); ++i) {
String currentWord = input.get(i);
for(int j=i+1; j<input.size(); ++j) {
String wordToCompare = input.get(j);
if(currentWord.equalsIgnoreCase(wordToCompare)) {
currentWord = wordToCompare;
newArrayList.set(i, wordToCompare);
newArrayList.set(j, "REMOVE_THIS_STRING");
}
}
}
/*
Code that goes through NewArrayList and removes
all Strings set to "REMOVE_THIS_STRING"
*/
If the ArrayList input is "interface", "list", "Primitive", "class", "primitive", "List", "Interface", "lIst", "Primitive"
, the expected output is "Interface", "lIst", "Primitive", "class"
, but instead I'm getting "Interface", "lIst", "Primitive", "class", "Primitive", "lIst"