Java program to remove duplicate words in given string


Program

//Program to remove Duplication of words
public class RemoveDuplicate
{

public static void main(String[] args)
{
String input="Welcome to Java Session Java Session Session Java"; //Input String
String[] words=input.split(" "); //Split the word from String
for(int i=0;i<words.length;i++) //Outer loop for Comparison
{
if(words[i]!=null)
{

for(int j=i+1;j<words.length;j++) //Inner loop for Comparison
{

if(words[i].equals(words[j])) //Checking for both strings are equal
{
words[j]=null; //Delete the duplicate words
}
}
}
}
for(int k=0;k<words.length;k++) //Displaying the String without duplicate words
{
if(words[k]!=null)
{
System.out.println(words[k]);
}

}
}
}

Output

Welcome

to

Java

Session




Related Post

Comments

Danish Sharma
         But If i will below take String Welcome java to java programming programming In that case program Won't Work.Kindly Check it....
Varun Bhardwaj
         Instead of taking words[I]. Take words [j]

©candidjava.com