Menu Bar

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Thursday, 19 January 2017

Write a Java Program to remove duplicate characters from String Without Using any API ?

Here we will learn the program to remove all the duplicate characters from string ,suppose we have string "Malayalam" then output will be "Malym". In given string we have duplicate character like 'a', and 'l' , so the output will become "Malym".

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class RemoveDuplicateCharacter {

 public static void remove(String str){
  String output = "";  
  String duplicateChar = "";
  
  for(int i=0;i<str.length();i++){
   if(output.contains(String.valueOf(str.charAt(i)))){
    duplicateChar+= str.charAt(i);
   }
   else{
    output+= str.charAt(i);
   }
  }
  System.out.println("Original String :\t"+str+"\nDuplicate Characters :\t"+duplicateChar+"\nOutput String :\t\t"+output);
 }
 public static void main(String[] args) {
  RemoveDuplicateCharacter.remove("Pushkarsa");
  System.out.println();
  RemoveDuplicateCharacter.remove("Malayalam");
 }

}

Output:

Original String : Pushkarsa
Duplicate Characters : sa
Output String : Pushkar

Original String : Malayalam
Duplicate Characters : aala
Output String : Malym


      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Share this Blog with yours Friends !!

No comments:

Post a Comment