Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Removing duplicate char's from an Array

    Okay this is the one part of my homework assignment that I just cannot figure out. I am getting a word from the program user. It has to be stripped down so that there are no duplicate letters, all lowercase, no symbols, etc. The only part that I can't get to work is removing duplicates.

    I have all of it but removing the duplicate letters. I've tried removing the duplicate letters in first using substrings but couldn't get it to work on anything other than adjacent letters.

    I've been trying to use arrays and for loops to do it now because I have to copy the processed word into a 2 dimensional array of 25 cells then add the rest of the alphabet to that array.

    If you've ever seen the playfair cipher then you'll have some idea of what the assignment is. The end result is a 5x5 grid with 25 letters of the alphabet in it (j is excluded) so if the keyword is lilly it would go liyab - cdefg - hkmno - pqrst - uvwxz.

    Any suggestions would be seriously appreciated! I'm about ready to beat my head into a wall over this method.

    Code:
    public char[] preProcess()
          {
             lowered = keyword.toLowerCase();
             stripped = lowered.replaceAll(pattern,"");
             key = stripped.replace('j','i');
             processing = new StringBuilder(key);
             tests = new char[processing.length()];
             processing.getChars(0,processing.length(),tests,0);
          	
             char[] tested = new char[tests.length];
             int size = 1;
             tested[0]=tests[0];
             for(int a = 0; a < tests.length; a++)
             {
                
                 
             }
          
          
                           
             return tested;
          }

  • #2
    New Coder
    Join Date
    Jan 2011
    Location
    India
    Posts
    31
    Thanks
    0
    Thanked 4 Times in 4 Posts
    Use the following alogo for removing duplicates
    PHP Code:

    int NewLength 
    1;
    for(
    i=1iLengthi++){

       for(
    j=0jNewLength j++)
       {

          if(array[
    i] == array[j])
          break;
       }

      if (
    j==NewLength )
          array[
    NewLength++] = array[i];

    use variables as per your requirement....

  • #3
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    659
    Thanks
    31
    Thanked 100 Times in 94 Posts
    This may give you some insight. I've added comments, though essentially this is the process: You read in a string, which I assume is stored in String word. Once that's down, I use the Java API to make the word all in lower case, then replace all non alpha characters using REGEX (Regular Expressions).

    After that's done, I, again using the Java API, convert this word to an array of characters. I then sort that alphabetically. Once that is down I iterate through the letters, keeping track of what the previous letter was, and only add unique ones. After that's done I copy it to a new array matching the length of the number of final characters (pretty much just for clarity purposes).

    Hope this helps point you in the right direction!

    After you've implemented this, you'll then just iterate through all the letters of the alphabet (you can just make an array containing all lower case alpha letters) and then check to see if the alpha character was already used (aka is it in the char[] playfair array). If it isn't, then print out that letter.

    PHP Code:
    import java.lang.*;
    import java.util.*;

    class 
    Test {

        public static 
    void mainString[] args ){

            
    // Let's assume the word has been read in by your script.
            
    String word "Applesauce1234";
            
            
    // Turn the word to all lower case
            
    word word.toLowerCase();
            
    // Replace all non lower case alpha characters
            
    word word.replaceAll"[^a-z]""" );
            
            
    // Turn the word into an array of letters
            
    char[] letters word.toCharArray();
            
    // Sort those letters alphabetically
            
    Arrays.sortletters );
            
            
    // Create array for adding in values
            
    char[] tempPlayfair = new char[letters.length];
            
    // Create temp char storage, initially set to NULL value
            
    char lastChar '\u0000';
            
    // Create incremental integer
            
    int j 0;
            
            for( 
    int i 0letters.lengthi++ ){
                if( 
    lastChar == letters[i] )
                    
    // Current letter matches the last one, don't store it
                    
    continue;
                
    // Add this character to the array, setting it as the last char
                
    tempPlayfair[j] = letters[i];
                
    lastChar letters[i];
                
    j++;
            }
            
            
    // Shrink the array and copy it to the final value
            
    char[] playfair = new char[j];
            
    playfair Arrays.copyOftempPlayfair);
            
            
    // Prints out the final result
            
    System.out.printlnplayfair );

        }
        

    Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
    My posts, comments, code, and suggestions reflect only my personal views.
    Web Portfolio and Code Snippets: http://shanechism.com


  •  

    Posting Permissions

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