Results 1 to 4 of 4

Thread: remove duplicate characters

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    121

    remove duplicate characters

    for the string "an example", i want to remove the dulipcate character from that string. and how do it remove the spaces between the words?

    in the end i should get:
    "anexmpl".

    thanks.

  2. #2
    Addicted Member TBeck's Avatar
    Join Date
    Apr 2006
    Location
    Ontario, Canada
    Posts
    254

    Re: remove duplicate characters

    removing spaces is as simple as

    Code:
    words = words.replaceAll(" ", "");
    If noone else gets the other part before i get up tommarrow I will help you out but i gotta sleep now.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    121

    Re: remove duplicate characters

    so how to remove duplicate character. i need that urgently.

    thanks.

  4. #4
    Addicted Member TBeck's Avatar
    Join Date
    Apr 2006
    Location
    Ontario, Canada
    Posts
    254

    Re: remove duplicate characters

    Ok, there are a few different ways this can be done, heres what I did:

    Code:
    import java.util.*;
    public class DublicateRemoval
    {
      public static void main (String[] args)
      {
        Scanner myInput = new Scanner(System.in);
        String words = myInput.nextLine();
        
        //removing spaces
        words = words.replaceAll(" ", "");
        
        //replace duplicates
        String temp = "";
        int position = 0;
        while(position < words.length())
        {
          temp = words.substring(position + 1);
          temp = temp.replaceAll(words.charAt(position) + "", "");
          //System.out.println(temp);
          //System.out.println(words.substring(0, position+1));
          words = words.substring(0, position + 1) + temp;
          position ++;
       }
        
        System.out.println(words);
      }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width