0

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);
}
}

}

  • Try [this](http://stackoverflow.com/questions/17967114/how-to-efficiently-remove-duplicates-from-an-array-without-using-set) – Jayfray Feb 23 '17 at 14:59
  • If i correctly understood, if your goal to have from input `A sailor went to sea sea sea, to see see see` output `A sailor went to sea, to see` Within one loop use Stack and push and if last added string is duplicate of previous just do not add, and for counting duplicated indices use another set to put the duplicated indices. For counting indices go from i -> N. And to grab words use two pointers, one will point to start of word, and other to end of word. – ernestk Feb 23 '17 at 15:01

1 Answers1

0

Your code is good, but if the order is critical, why not use a LinkedHashSet? Add the elements to the linkedhashset, then to get them back in the order they were added, simply get the entries as toString(). You'll have a comma delimiting the returned words, but they can easily be removed. It will look like "[a,sailor,went,to,sea,see,what ...]"

 static ArrayList<String> removeDuplicates(String[] list) {
      // Record encountered Strings in HashSet.
      LinkedHashSet<String> set = new LinkedHashSet<>();
      // Loop over argument list.
      for (String item : list) {
          // Left this if statement in for clarity (!needed)
          if (!set.contains(item)) {
              set.add(item);
           }
      }

      String result= set.toString(); 
      return(result.split(",");
   }
Dakoda
  • 175
  • 7