0

I am trying to remove duplicates in my string array though it's not working, I uses Split String to get my string in an array and then used a counter method to count the duplicates. I don't understand what I did wrong

public class Program {
 public static void uniqWords(String s){
    String[] sentence = s.split(" ");
    int[] counter = new int[sentence.length];
    for(int i=0; i< sentence.length; i++){
    for(int j=i+1; j<sentence.length; j++){
        if(sentence[i] == sentence[j] ){
            counter[i] =1;
        }
     }
    }//
    for(int i=0; i<counter.length; i++){
       System.out.print(counter[i] + ",");
    }
   for(int i =0; i<sentence.length; i++){
     if(counter[i] == 1){
        sentence[i] = "";
      }
    }
//print
    for(int i=0; i<sentence.length; i++){
      System.out.print(sentence[i]);
      System.out.print(" ");
     }
  //

  }


  public static void main(String[] args) {
      // TODO Auto-generated method stub

      uniqWords("Spring in in Paris");

    }
}
Draken
  • 3,134
  • 13
  • 34
  • 54
Micael Illos
  • 467
  • 2
  • 15
  • You're not really counting anything. You're setting a counter to ``1`` each time you find something. Also: debug your program. Finding mistakes such as this is what the debugger is for. Spoiler: you don't compare Strings with ``==`` in java. – f1sh Feb 07 '17 at 13:52
  • @KevinEsche - And having converted the array to a Set, how do you propose to reconstruct the sentence? Since that's what "removing duplicates *from a sentence*" would entail? – Mark Adelsberger Feb 07 '17 at 13:54
  • @MarkAdelsberger by transforming it back into a `String` array? – SomeJavaGuy Feb 07 '17 at 14:01
  • Is there a better way of doing this? Without using set – Micael Illos Feb 07 '17 at 14:02

2 Answers2

5

The most critical problem is that you're trying to compare two strings' values using ==, which won't work. You need to use the equals() method on one of the strings to compare it to the other.

Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52
3

Convert your array into a Set. A set does not allow duplicates, so it will be removed:

    String[] sentence = s.split(" ");
    Set<String> set = new HashSet<String>(Arrays.asList(sentence));
SCouto
  • 7,808
  • 5
  • 32
  • 49
  • ...which neither addresses what the OP did wrong nor achieves what they said they want to do – Mark Adelsberger Feb 07 '17 at 13:52
  • 3
    From the OP: "I am Trying to remove duplicates in my string array" He is counting as a method to achieve the removal, not as an objective of its code – SCouto Feb 07 '17 at 13:53
  • While this is not an answer, I personally found it helpful. As soon as I read it, I went "oh duh!" – toshiomagic Feb 07 '17 at 13:55
  • @SCouto : I never said counting was the objective. But if he converts the array to a set, he has an unordered collection of words with duplicates removed - not a sentence with duplicates removed. – Mark Adelsberger Feb 07 '17 at 13:56
  • @MarkAdelsberger [just going to leave this link here](http://docs.oracle.com/javase/6/docs/api/java/util/LinkedHashSet.html) – SomeJavaGuy Feb 07 '17 at 14:04
  • Which, if he had specified that, would be relevant. But his code sample very explicitly doesn't use that as I notice. – Mark Adelsberger Feb 07 '17 at 14:08