Program to remove duplicates from a string
class sample {
public static void main(String[] args) {
String str="hello how are you hello how are you";
String[] each=str.split(" ");
String res="";
for (int i=0;i<each.length ; i++) {
for(int j=i+1;j<each.length;j++){
if(each[i].equals(each[j])){
each[j]="";
}
}
res=res+ " "+each[i];
}
for (String k :each ) {
System.out.println(k);
}
System.out.println(res);
}
}
public static void main(String[] args) {
String str="hello how are you hello how are you";
String[] each=str.split(" ");
String res="";
for (int i=0;i<each.length ; i++) {
for(int j=i+1;j<each.length;j++){
if(each[i].equals(each[j])){
each[j]="";
}
}
res=res+ " "+each[i];
}
for (String k :each ) {
System.out.println(k);
}
System.out.println(res);
}
}
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment