+ Reply to Thread
Results 1 to 9 of 9

Thread: deleteRepeats() method in a char array

  1. #1
    Newbie mahoni is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    9

    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. #2
    Programmer Sinipull will become famous soon enough Sinipull's Avatar
    Join Date
    Jun 2009
    Location
    Estonia
    Age
    21
    Posts
    165

    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]

  3. #3
    Newbie mahoni is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    9

    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.

  4. #4
    Programmer Sinipull will become famous soon enough Sinipull's Avatar
    Join Date
    Jun 2009
    Location
    Estonia
    Age
    21
    Posts
    165

    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.

  5. #5
    Newbie mahoni is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    9

    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

  6. #6
    Programmer Sinipull will become famous soon enough Sinipull's Avatar
    Join Date
    Jun 2009
    Location
    Estonia
    Age
    21
    Posts
    165

    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.

  7. #7
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,662
    Blog Entries
    57

    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.
    CodeCall Blog | CodeCall Wiki | Shareware
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  8. #8
    Programmer Sinipull will become famous soon enough Sinipull's Avatar
    Join Date
    Jun 2009
    Location
    Estonia
    Age
    21
    Posts
    165

    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 08:43 AM.

  9. #9
    Newbie mahoni is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    9

    Re: deleteRepeats() method in a char array

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

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

     

Similar Threads

  1. Char arrays in C
    By outsid3r in forum C Tutorials
    Replies: 4
    Last Post: 07-28-2009, 03:38 PM
  2. Double buffering, movement, and collision detection.
    By farrell2k in forum Java Tutorials
    Replies: 5
    Last Post: 06-06-2009, 09:22 AM
  3. Replies: 0
    Last Post: 11-27-2008, 01:30 PM
  4. Char Arrays
    By ahmed in forum C and C++
    Replies: 65
    Last Post: 11-15-2008, 03:20 PM
  5. char array
    By RobotGymnast in forum C and C++
    Replies: 2
    Last Post: 10-29-2008, 04:48 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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