Removing Duplicates
Given a string as input with duplicates. You has to print the output without duplicates and also in same order as input.
INPUT
"100 apples"
OUTPUT
10 aples
JAVA Solution
import java.util.*;
public class Hello {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String str = s.nextLine();
String []str1 = str.split(" ");
int i, j, k;
for(i=0;i<str1.length;i++)
{
LinkedHashSet<Character>set = new LinkedHashSet<Character>();
for(j=0;j<str1[i].length();j++)
{
set.add(str1[i].charAt(j));
}
for(Character ch:set)
System.out.print(ch);
System.out.println();
}
}
}
If anyone come up with better solution leave it in comment.
Keep Programming...!!
INPUT
"100 apples"
OUTPUT
10 aples
JAVA Solution
import java.util.*;
public class Hello {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String str = s.nextLine();
String []str1 = str.split(" ");
int i, j, k;
for(i=0;i<str1.length;i++)
{
LinkedHashSet<Character>set = new LinkedHashSet<Character>();
for(j=0;j<str1[i].length();j++)
{
set.add(str1[i].charAt(j));
}
for(Character ch:set)
System.out.print(ch);
System.out.println();
}
}
}
If anyone come up with better solution leave it in comment.
Keep Programming...!!
Comments
Post a Comment