Java Topics

Remove repeated words from String array

public class RemoveRepeatedWords {

public static void main(String[] args) {

String[] s= {"hari","ram","hari","krish","ram"};

int k;

int n=s.length;

for(int i=0;i<n;i++) {

for(int j=i+1;j<n;j++) {

if(s[i]==s[j]) {

k=j;

while(k<n-1) {

s[k]=s[k+1];

k++;

}

n--;

j--;

}

}

}

      for(int j=0;j<n;j++) {

      System.out.println(s[j]+" ");

      }

}

}

Output:

hari 
ram 
krish