Closed Thread
Results 1 to 9 of 9

Thread: deleteRepeats() method in a char array

  1. #1
    mahoni is offline Newbie
    Join Date
    Jun 2009
    Posts
    9
    Rep Power
    0

    deleteRepeats() method in a char array

    Hi everyone,

    I want to write a static method which will take a partially filled char array and an int size which gives the number of positions used in array. This method will delete all repeated elements from the array and return the new size of the array.For example following code:

    char a[10];
    a[0] = 'a';
    a[1] = 'b';
    a[2] = 'a';
    a[3] = 'c';
    int size = 4;
    size = deleteRepeats(a, size);

    After executing this code, the value of a[0] is 'a', the value of a[1] is 'b', the value of a[2] is 'c', and the value of size is 3.

    I wrote following code for this problem and it does not work properly, it gives wrong results after calling the code. What is the problem with following code:


    Code:
     
    public class L3S1 {
        
        public static void main(String[] args) {
        	
        	char [] a=new char[10];
        	a[0]='a';
        	a[1]='b';
        	a[2]='d';
        	a[3]='c';
        	a[4]='c';
        	a[5]='d';
        	int newSize=deleteRepeats(a,6);
        	System.out.println(newSize);
    
        }
        
        public static int deleteRepeats(char[]array,int size){
        	int result=size;
        	
        	for(int i=0;i<size;i++){
        		for(int j=i+1;j<size;j++){
        			if(array[i]==array[j]){
        				for(int k=j+1;k<size;k++){
        					array[j]=array[k];
        					result--;
        				}
        				
        			}
        		}
        	}
        	return result;
        		
        }
    }

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Sinipull's Avatar
    Sinipull is offline Programming Expert
    Join Date
    Jun 2009
    Location
    Tallinn, Estonia, Estonia
    Posts
    383
    Rep Power
    13

    Re: deleteRepeats() method in a char array

    After calling the function, your array is "abdcdd".. so it's definitely not right.

    that's how i would do it:

    Code:
    import java.util.ArrayList;
    
    public class L3S1 {
        
        public static void main(String[] args) {
        	ArrayList<Character> array = new ArrayList<Character>();
        	array.add('a');
        	array.add('c');
        	array.add('b');
        	array.add('d');
        	array.add('b');
        	array.add('d');
        	array.add('c');
        	array.add('c');
        	array.add('b');
        	array.add('d');
        	array.add('c');
        	array.add('c');
        	array.add('a');
        	
        	    	
        	int newSize=deleteRepeats(array);
        	System.out.println(newSize);
        	System.out.println(array);
        }
        
        public static int deleteRepeats(ArrayList<Character> array){
        	for(int i = 0; i < array.size(); i++){
    	      if(array.lastIndexOf(array.get(i)) != i){	    			
    	    	   array.remove(array.lastIndexOf(array.get(i)));    			
    	    	   i--;
    	      }
    	}    	    	
        	return array.size();
        		
        }
    }
    prints out:

    Code:
    4
    [a, c, b, d]

  4. #3
    mahoni is offline Newbie
    Join Date
    Jun 2009
    Posts
    9
    Rep Power
    0

    Re: deleteRepeats() method in a char array

    Ok, thanks but it is of course much more more more ... and more easier to do it with ArrayList. You just make use of the methods of ArrayList and find a way to solve the problem.

    But the main problem here is to do it with Arrays and I actually do not care about the elements that come after the (size)th element.

  5. #4
    Sinipull's Avatar
    Sinipull is offline Programming Expert
    Join Date
    Jun 2009
    Location
    Tallinn, Estonia, Estonia
    Posts
    383
    Rep Power
    13

    Re: deleteRepeats() method in a char array

    Quote Originally Posted by mahoni View Post
    But the main problem here is to do it with Arrays and I actually do not care about the elements that come after the (size)th element.
    Ok, i see, trying to find a way.

  6. #5
    mahoni is offline Newbie
    Join Date
    Jun 2009
    Posts
    9
    Rep Power
    0

    Re: deleteRepeats() method in a char array

    Quote Originally Posted by Sinipull View Post
    Ok, i see, trying to find a way.
    Thanks for your interest

  7. #6
    Sinipull's Avatar
    Sinipull is offline Programming Expert
    Join Date
    Jun 2009
    Location
    Tallinn, Estonia, Estonia
    Posts
    383
    Rep Power
    13

    Re: deleteRepeats() method in a char array

    Code:
    public class L3Sl {
        
        public static void main(String[] args) {
        	
        	char [] a=new char[10];
        	a[0]='a';
        	a[1]='b';
        	a[2]='d';
        	a[3]='c';
        	a[4]='c';
        	a[5]='d';
        	
        	
        	int newSize=deleteRepeats(a);
        	System.out.println(newSize);
        	System.out.print(a);
        	
        }
        
        public static int deleteRepeats(char[]array){
        	int size = 0;   	
        	    	
        	for(int i=0; i<array.length; i++){
        		for(int j=i+1; j<array.length; j++){
        			if(array[j] == array[i]) array[j] = 0;
        		}
        	}
        	for(int i=0; i<=array.length; i++){
        		if(array[i] != 0 ) size++;
        	}
        	
        	return size;	
        }
    }
    prints

    Code:
    4
    abdc
    for the future... it is easy to test which characters are unused in char array, because their integer or ASCII value is 0.

  8. #7
    Join Date
    Jul 2006
    Posts
    16,525
    Blog Entries
    75
    Rep Power
    144

    Re: deleteRepeats() method in a char array

    Just a thought: in your deleteRepeats function, you are using size as a loop limit, when the size of your array is actually changing. You change result to reflect those changes, but not size. That just feels like a bug waiting to happen.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  9. #8
    Sinipull's Avatar
    Sinipull is offline Programming Expert
    Join Date
    Jun 2009
    Location
    Tallinn, Estonia, Estonia
    Posts
    383
    Rep Power
    13

    Re: deleteRepeats() method in a char array

    I think i finally understood what you want.

    Code:
    public class Test {
        
        public static void main(String[] args) {
        	
        	char [] a=new char[10];
        	a[0]='a';
        	a[1]='b';
        	a[2]='c';
        	a[3]='c';
        	a[4]='c';
        	a[5]='d';
    
        	
        	
        	int newSize=deleteRepeats(a, 3);
        	System.out.println(newSize);
        	
        	
        }
        
        public static int deleteRepeats(char[]array, int size){
        	int count=0;
        	
        	char arrayTemp[] = new char[array.length];
    
        	// lets make a copy of the array, so we won't change it.
        	for(int i=0; i<size; i++){
        		arrayTemp[i] = array[i];
        	}
        	
        	
        	//clear all slots with repeating chars
        	for(int i=0; i<size; i++){
        		for(int j=i+1; j<size; j++){
        			if(arrayTemp[j] == arrayTemp[i]) arrayTemp[j] = 0;
        		}
        	}
        	
        	//lets "cut" the caps inside of the array
        	char [] a=new char[arrayTemp.length];
        	int charcount=0;
        	for(int i=0; i<size; i++){
        		if(arrayTemp[i] != 0){
        			a[charcount] = arrayTemp[i];
        			charcount++;
        		}
        	}
        	for(int i=0; i<arrayTemp.length; i++){
        		arrayTemp[i] = a[i];    		
        	}
        	
        	
        	// count the chars left
        	for(int i=0; i<size; i++){
        		if(arrayTemp[i] != 0 ) count++;
        	}
        	
        	return count;	
        }
    }
    Last edited by Sinipull; 07-05-2009 at 06:43 AM.

  10. #9
    mahoni is offline Newbie
    Join Date
    Jun 2009
    Posts
    9
    Rep Power
    0

    Re: deleteRepeats() method in a char array

    Thank you very much, thats definitely what I want to do

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: 09-22-2011, 04:59 PM
  2. Reading from file char by char in array
    By cypro in forum C and C++
    Replies: 7
    Last Post: 08-10-2011, 12:30 AM
  3. Conversion from char array to int array.
    By TheUmer in forum C and C++
    Replies: 6
    Last Post: 03-26-2010, 05:33 PM
  4. Struct with char* to char array?
    By manux in forum C and C++
    Replies: 1
    Last Post: 03-17-2009, 11:19 PM
  5. char array
    By RobotGymnast in forum C and C++
    Replies: 2
    Last Post: 10-29-2008, 02:48 PM

Tags for this Thread

Bookmarks

Posting Permissions

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