Given a string, remove subsequent duplicate characters until we get a unique set of characters. Ex: input ==> apple ; expected output ==> ale Ex: input ==>appapple; expected output ==>le The input will be in char array. char[] removeDupes(Char[] inputString){ }
Anonymous
/*Given a string, remove subsequent duplicate characters until we get a unique set of characters. Ex: input ==> apple ; expected output ==> ale Ex: input ==>appapple; expected output ==>le */ import java.util.ArrayList; public class RemoveDuplicateCharacters { public static void main(String[] args) { // TODO Auto-generated method stub String S = "appapple"; int sLen = S.length(); ArrayList alStr = new ArrayList(); ArrayList rejectStr = new ArrayList(); Boolean flag; for(int i =0;i< sLen;i++) { flag = true; for(int j=i+1;j< sLen; j++) { if(S.charAt(j)==S.charAt(i) || rejectStr.contains(S.charAt(i))) { flag = false; rejectStr.add(S.charAt(i)); break; } //If condition } //for loop j if(flag == true) { alStr.add(S.charAt(i)); } //If condition } //For Loop i System.out.println(alStr); } }
Check out your Company Bowl for anonymous work chats.