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...!! 

Comments

Popular posts from this blog

Balanced Binary Tree

First Unique Character in a String

Majority Element

Smallest Range II