Friday, September 11, 2015

Write a Java program to get unique characters from String

In this post, we are going to see how to get unique characters from string.
There are several ways to get unique characters from string. In this post, i have implemented two ways.
1. using boolean array.
2. using map.
1. Using Boolean array: In this technique, first we need to convert string into char array. After that we need to iterate all characters. While iterating we need to check that character is already added to array or not. If it is not added then we need to add that character to boolean array and append that character to stringbuilder. If it is already added that character to boolean array, skipping that character to add boolean array.
Code: 
public static String getUniqueCharsUsingBooleanArray(String input) {
        if (input == null || input.length() == 0)
            return input;

        boolean uniqueChars[] = new boolean[256];
        StringBuilder sb = new StringBuilder();
        char chars[] = input.toCharArray();
        for (char ch : chars) {
            if (!uniqueChars[ch]) {
                uniqueChars[ch] = true;
                sb.append(ch);
            }
        }
        return sb.toString();
}
2. Using Map: In this technique, first we need to convert string into char array. After that we need to iterate each character. While iterating we need to check that character is already added or not. If it is not added, adding that character to map and making count is 1. If it is already added then increment that character with count +1. Once all iteration is done then we need to iterate map and append to that character to stringbuilder. Finally convert this stringbuilder to string using toString() method.
Code:
public static String getUniqueCharsUsingMap(String input) {
        if (input == null || input.length() == 0)
            return input;

        Map<Character, Integer> map = new LinkedHashMap<>();
        char chars[] = input.toCharArray();
        for (char ch : chars) {
            if (map.get(ch) == null) {
                map.put(ch, 1);
            } else {
                map.put(ch, map.get(ch) + 1);
            }
        }
        StringBuilder sb = new StringBuilder();
        for (Character uniqueChar : map.keySet()) {
            sb.append(uniqueChar);
        }
        return sb.toString();
}

Program:
package com.ranga;

import java.util.LinkedHashMap;
import java.util.Map;

public class UniqueCharecters {

    public static void main(String[] args) {
        String input = "my name is ranga reddy";
        System.out.println("Unique Characters using way1: " + getUniqueCharsUsingMap(input));
        System.out.println("Unique Characters using way2: " + getUniqueCharsUsingBooleanArray(input));
    }

    public static String getUniqueCharsUsingBooleanArray(String input) {
        if (input == null || input.length() == 0)
            return input;

        boolean uniqueChars[] = new boolean[256];
        StringBuilder sb = new StringBuilder();
        char chars[] = input.toCharArray();
        for (char ch : chars) {
            if (!uniqueChars[ch]) {
                uniqueChars[ch] = true;
                sb.append(ch);
            }
        }
        return sb.toString();
    }

    public static String getUniqueCharsUsingMap(String input) {
        if (input == null || input.length() == 0)
            return input;

        Map<Character, Integer> map = new LinkedHashMap<>();
        char chars[] = input.toCharArray();
        for (char ch : chars) {
            if (map.get(ch) == null) {
                map.put(ch, 1);
            } else {
                map.put(ch, map.get(ch) + 1);
            }
        }
        StringBuilder sb = new StringBuilder();
        for (Character uniqueChar : map.keySet()) {
            sb.append(uniqueChar);
        }
        return sb.toString();
    }
}
Output:
Unique Characters using way1: my naeisrgd
Unique Characters using way2: my naeisrgd
Happy Learning ....!

0 comments: