Find duplicate words in a string
Page 1 of 1
Find duplicate words in a string
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);
}
}
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);
}
}
Similar topics
» Reverse words in a string
» Given a document, how to find pairs of words with same charactors but different order.
» Reverse a String
» String Matching KMP
» Find a point in an array where sum of left side array members(wrt to that point) and right side(wrt to that point) are equal..in other words equilibrium point.
» Given a document, how to find pairs of words with same charactors but different order.
» Reverse a String
» String Matching KMP
» Find a point in an array where sum of left side array members(wrt to that point) and right side(wrt to that point) are equal..in other words equilibrium point.
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum