Micael Illos Micael Illos - 1 year ago 74
Java Question

Duplicate String Array in java

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] = "";
for(int i=0; i<sentence.length; i++){
System.out.print(" ");


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

uniqWords("Spring in in Paris");


Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download