Find duplicate words in a string

View previous topic View next topic Go down

Find duplicate words in a string

Post  Admin on Sat Nov 05, 2011 5:23 pm

import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;


public class Duplicateword {
public String check(String str){
String output="";
String[] ch=str.split(" ");
Hashtable hash1=new Hashtable();
for(int i=0;i<ch.length;i++){
if(!hash1.containsKey(ch[i])){
hash1.put(ch[i], 1);
}
else{
hash1.put(ch[i], ((Integer)hash1.get(ch[i])).intValue()+1);
}
}
Iterator in=hash1.entrySet().iterator();
while(in.hasNext()){
Map.Entry m=(Map.Entry)in.next();
if((Integer)(m.getValue())==2){
output+=m.getKey();
}
}
return output;
}

public static void main(String[] args){
Duplicateword d=new Duplicateword();
String temp=d.check("word word is a good assign is a good bit");
System.out.println(temp);
}
}

Admin
Admin

Posts : 131
Join date : 2011-08-16

View user profile http://codefornongeek.forumotion.com

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum