Find out duplicate words in a given string & remove them

Chikul
Posted by Chikul under Others category on | Views : 7331
import java.io.*;
import java.lang.*;
import java.util.*;

public class Remove_Duplicate
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a String : ");
String s=br.readLine().toString();
int i=0;
ArrayList ar=new ArrayList();

StringTokenizer st=new StringTokenizer(s," ");
while(st.hasMoreTokens())
{
ar.add(st.nextToken());

}
int l=ar.size();
String s1[]=new String[l];


Iterator itr=ar.iterator();
while(itr.hasNext())
{
s1[i]=itr.next().toString();

i++;
}

for(i=0;i<l;i++)
{
for(int j=i+1;j<l;j++)
{
if(s1[i].equalsIgnoreCase(s1[j]))
{
s1[j]="null";

}
}
}
System.out.println();
for(i=0;i<l;i++)
{
if(s1[i].equals("null"))
{

}
else
{
System.out.print(" "+s1[i]+" ");
}
}

}}

Comments or Responses

Login to post response