Header Ads Widget

Responsive Advertisement

Remove All the Duplicate from a String in JAVA

 



import java.util.*;

public class Recursion2 {
 public static boolean[] map = new boolean[26];

    public static void removeDuplicate(String str, int indx, String newString) {
        if (indx == str.length()) {
            System.out.println(newString);
            return;
        }
        char currentChar = str.charAt(indx);
        if (map[currentChar - 'a'] == true) {
            removeDuplicate(str, indx + 1, newString);
        } else {
            newString += currentChar;
            map[currentChar - 'a'] = true;
            removeDuplicate(str, indx + 1, newString);
        }
    }

 public static void main(String[] args) {
       
        String str = "aabbcc";
       
         removeDuplicate(str, 0, "");
       
    }
}

Post a Comment

0 Comments