-2

I am a little confused how to approach this problem. The userKeyword is passed as a parameter from a previous section of the code. My task is to remove any duplicate chars from the inputted keyword(whatever it is). We have just finished while loops in class so some hints regarding these would be appreciated.

    private String removeDuplicates(String userKeyword){
        String first = userKeyword;

        int i = 0;
        while(i < first.length())
        {
            if (second.indexOf(first.charAt(i)) > -1){ 
            }    
            i++;
        return ""; 

Here's an update of what I have tried so far - sorry about that.

Tuxino
  • 39
  • 1
  • 1
  • 6
  • 2
    Can you post what you've tried so far? – William Morrison Aug 17 '13 at 20:22
  • 1
    The details of your task are unclear from your post. More information would be helpful. Additionally, StackOverflow.com should not be used during class to solve in class problems without explicit permission to use outside resources/help for this assignment. Academic integrity is very important. Perhaps you should request assistance from a TA or Instructor if online resources have not been explicitly allowed. – Andrew Ring Aug 17 '13 at 20:30

3 Answers3

0

This is the perfect place to use java.util.Set, a construct which is designed to hold unique elements. By trying to add each word to a set, you can check if you've seen it before, like so:

static String removeDuplicates(final String str)
{
    final Set<String> uniqueWords = new HashSet<>();
    final String[] words = str.split(" ");
    final StringBuilder newSentence = new StringBuilder();
    for(int i = 0; i < words.length; i++)
    {
        if(uniqueWords.add(words[i]))
        {
            //Word is unique
            newSentence.append(words[i]);
            if((i + 1) < words.length)
            {
                //Add the space back in
                newSentence.append(" ");
            }
        }
    }
    return newSentence.toString();
}

public static void main(String[] args)
{
    final String str = "Words words words I love words words WORDS!";
    System.out.println(removeDuplicates(str)); //Words words I love WORDS!
}
MrLore
  • 3,759
  • 2
  • 28
  • 36
  • This was my first thought too. I didn't use advanced structures or generics as he's probably not learned them yet. Even removed my stringbuilder as beginners are confused by it. As a result my solution is more wasteful than yours but very simple. It was nice practice. – William Morrison Aug 17 '13 at 20:34
0

For future reference, StackOverflow normally requires you to post what you have, and ask for suggestions for improvement.

As its not an active day, and I am bored I've done this for you. This code is pretty efficient and makes use of no advanced data structures. I did this so you could more easily understand it.

Please do try to understand what I'm doing. Learning is what StackOverflow is for.

I've added comments in the code to assist you in learning.

private String removeDuplicates(String keyword){
    //stores whether a character has been encountered before
    //a hashset would likely use less memory.
    boolean[] usedValues = new boolean[Character.MAX_VALUE];

    //Look into using a StringBuilder. Using += operator with strings
    //is potentially wasteful.
    String output = "";

    //looping over every character in the keyword...
    for(int i=0; i<keyword.length(); i++){
        char charAt = keyword.charAt(i);
        //characters are just numbers. if the value in usedValues array 
        //is true for this char's number, we've seen this char.
        boolean shouldRemove = usedValues[charAt];
        if(!shouldRemove){
            output += charAt;
            //now this character has been used in output. Mark that in 
            //usedValues array
            usedValues[charAt] = true;
        }
    }
    return output;
}

Example:

//output will be the alphabet.
System.out.println(removeDuplicates(
    "aaaabcdefghijklmnopqrssssssstuvwxyyyyxyyyz"));
William Morrison
  • 10,953
  • 2
  • 31
  • 48
0

Have a look at this answer.

You might not understand this, but it does the job (it cleverly uses a HashSet that doesn't allow duplicate values).

I think your teacher might be looking for a solution using loops however - take a look at William Morisson's answer for this.

Good luck!

Community
  • 1
  • 1
James Williams
  • 678
  • 4
  • 14