1

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"

SawyerZ
  • 21
  • 1
  • 1
    You can get the position of the duplicate strings and remove those elements. Or you can add the elements to a Set and reconstruct the Array from that Set. – Pritam Banerjee Apr 19 '19 at 20:23

2 Answers2

1

To remove the duplicates you can use Java 8 stream.distict() Like this:

List<Integer> newArrayList= input.stream()
     .map(String::toLowerCase)
     .distinct()
     .collect(Collectors.toList());
}

It is case sensitive so you have to map to lowercase before.

To capitalize first letter of each result distinct word, you need to add:

.map(name -> name.substring(0, 1).toUpperCase() + name.substring(1))

in the stream porcessing. So it will be:

List<Integer> newArrayList= input.stream()
     .map(String::toLowerCase)
     .distinct()
     .map(word-> word.substring(0, 1).toUpperCase() + word.substring(1))
     .collect(Collectors.toList());
}
Laguh
  • 621
  • 2
  • 10
  • 23
  • That is how you would do it, but IIUC here the requirement is to remove the elements from the original list rather than create a new list with the duplicates removed. – daniu Apr 19 '19 at 21:06
  • The OP is creating a new list so it must be okay. – WJS Apr 19 '19 at 21:16
1

This changes the relative order of the kept values, but that was not provided as a requirement.

  List<String> input = new ArrayList<>(List.of("interface",
        "list",
        "Primitive",
        "class",
        "primitive",
        "List",
        "Interface",
        "lIst",
        "Primitive"));

  for (int k = 0; k < input.size(); k++) {
     String word = input.get(k);
     // remove them all
     for (int i = 0; i < input.size(); i++) {
        if (input.get(i).equalsIgnoreCase(word)) {
           word = input.remove(i);
        }
     }

     //add in the last one removed.
     input.add(0, word);
  }

  System.out.println(input);
WJS
  • 36,363
  • 4
  • 24
  • 39