I currently have this code that is fully functioning in Java. It takes a string, turns it into an array and removes all of the duplicates. I decided to use the string "a sailor went to sea sea sea to see what he could see see see but all that he could see see see was the bottom of the deep blue sea sea sea". I used this as it has a large number of duplicates.
To add to the code I would like to be able to get the positions of all the elements in the array, I believe the correct method is through nested loops but I am not sure how to accomplish this. If anyone has some guidance or even general ideas i would be great full.
Here is the current code:
static ArrayList<String> removeDuplicates(String[] list) {
// Store unique items in result.
ArrayList<String> result = new ArrayList<>();
// Record encountered Strings in HashSet.
HashSet<String> set = new HashSet<>();
// Loop over argument list.
for (String item : list) {
// If String is not in set, add it to the list and the set.
if (!set.contains(item)) {
result.add(item);
set.add(item);
}
}
return result;
}
public static void main(String[] args) {
String sea1 = "A sailor went to sea sea sea, to see what he could see see see, but all that he could see see see, was the bottom of the deep blue sea sea sea";
String sea2 = sea1.toLowerCase();
String sea3 = sea2.replaceAll("[\.:;,\"!\?]", " "); //remove punctuation + sets to lower case
String sea4 = sea3.replaceAll(" ", " ");
String sea5 = sea4.replaceAll(" ", ",");
String sea6 = sea5.replaceAll("'", " ");
String sea7 = sea6.replaceAll(" ", "");
System.out.println("Here is the string: " + sea7);
String[] sealist = sea7.split(",");
System.out.println("Here is the string 'end' with duplicates removed: ");
// Remove duplicates from ArrayList of Strings.
ArrayList<String> unique = removeDuplicates(sealist);
for (String element : unique) {
System.out.println("- " + element);
}
}
}