1

I have an array of characters. I should remove any duplicate characters. I did comparison between the string elements with an array of alphabets. There is another array that acts as counter for any duplictae alphabet. If any character has been found for more than once, I should remove the duplicated character and shif the elements to left. Below is the code. I get error at the line with the comment. Java needs the left hand side of the assignment to be variable. Can you help?

import java.util.ArrayList;
import java.util.Scanner;

public class removeduplicate {

    public static void main (String args[])
    {
        Scanner s=new Scanner(System.in);
        String str="abbsded";

        String myChars="abcdefghijklmnopqrstuvwxyz";
        int[] myCounter=new int[26];

        for (int i=0; i<str.length(); i++)
        {
            for(int j=0; j<myChars.length(); j++)
            {
                if(str.charAt(i)==myChars.charAt(j))
                {
                    myCounter[j]++;
                    if (myCounter[j]>1)
                    {
                        System.out.println("duplication found in "+ str.charAt(i));
                        for(int h=i; h<str.length();h++)
                        {
                            //this line does not work. 
                            str.charAt(h)=str.charAt(h-1);
                        }
                    }

                }
            }

        }//end for

    }

}
user6875880
  • 651
  • 1
  • 7
  • 17
  • Could you show us the error? Also you have a String of characters and an alphabet String. An array is your int [] myCounter – Ishnark Feb 02 '17 at 21:12
  • 1
    you are looping way to much, you should use a hashmap to keep track of your counts – RAZ_Muh_Taz Feb 02 '17 at 21:14
  • Also, if all you want to is remove duplicates from `str`, this might help: http://stackoverflow.com/questions/4989091/removing-duplicates-from-a-string-in-java – Ishnark Feb 02 '17 at 21:17

1 Answers1

0

You can use a Hashmap to keep track of the characters you have run into during your implementation. Then just increment every time you see a character in the alphabet. Only adding a letter to the returned string if the character hasn't been seen before.

public static String removeDuplicates(String input)
{
    String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    input = input.toUpperCase();
    HashMap<Character, Integer> charData = new HashMap<>();

    //create the map to store counts of all the chars seen
    for(int i = 0; i < alphabet.length(); i++)
        charData.put(alphabet.charAt(i), 0);

    String cleanedInput = "";

    for(int index = 0; index < input.length(); index++)
    {
        char letter = input.charAt(index);
        if(charData.containsKey(letter))
        {
            charData.put(letter, charData.get(letter) + 1);
            //if count is 1 then its the first time we have seen it
            if(charData.get(letter) == 1)
            {
                cleanedInput += letter;
            }
        }
    }
    return cleanedInput.toLowerCase();
}

Example Call

public static void main(String[] args) {
    System.out.println(removeDuplicates("abbsded"));
    System.out.println(removeDuplicates("abbsded!!!"));
    System.out.println(removeDuplicates("hahahahahahahahahah"));
}//main method

Output

absde
absde
ha

Note: it only returns the characters once and no characters that aren't in the alphabet are considered in the new trimmed String.

RAZ_Muh_Taz
  • 4,059
  • 1
  • 13
  • 26