1
Sponsored Links


Ad by Google
In our previous post, we have seen one of the very popular java programming interview question"How to balanced parenthesis with o(n) time and space complexity" here.
And in this post again we are going to show you simple java program to remove duplicate characters from a String with o(n) performance. Of course there are lots of way to achieve this and we are using one of them. This type of questions always asked in your coding interview not only for freshers but for experienced candidates also. Sometimes interviewer wants to check your coding skills and he/she may asked you to write a simple program to solve this problem on paper.

UniqueChar.java

package com.javamakeuse.poc;

public class UniqueChar {

	public static String removeDuplicates(String input) {

		// container to hold unique characters
		StringBuilder uniqueChar = new StringBuilder();

		for (int i = 0; i < input.length(); i++) {

			String si = input.substring(i, i + 1);

			if (uniqueChar.indexOf(si) == -1) {
				uniqueChar.append(si);
			}
		}
		return uniqueChar.toString();
	}

	public static void main(String[] args) {
		System.out.println(removeDuplicates("Book"));
		System.out.println(removeDuplicates("Look"));
		System.out.println(removeDuplicates("blaa bla bla"));
	}
}

OUT PUT:
Bok
Lok
bla

Sponsored Links

1 comments:

  1. Learning new technolgoy would help oneself at hard part of their career. And staying updated is the only way to survive in current position. Your content tells the same. Thanks for sharing this information in here. Keep blogging like this.

    JAVA J2EE Training Institutes in Chennai | JAVA Training | JAVA Course in Chennai | Hadoop training in chennai

    ReplyDelete