Thread: delete same array element in C

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    9

    delete same array element in C

    Hi all;

    I am so new in C.I have problem about array .I want to remove same element in array but i dont know how can i do. Can anyone help me

    example : 1,2,3,5,67,31,13,5,2,1,1,4,
    the program should produce -->>1,2,3,5,67,13,31

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    You can't "remove" elements from an array in C. There are a few ways to go about doing what you want, though.

    You can use memmove() to shuffle around the elements of the array. This is annoying but will be close to the results you want (but you have to keep track of how many elements you "remove", because C's arrays cannot change size). If it's actually a pointer to some space you allocated, then you could use realloc() after memmove() and resize it; but you'd still have to keep track of the size.

    You can mark deleted elements with an "invalid" value, such as -1; that is, assuming there is a value that you can consider invalid.

    You can copy the elements you want to keep into a new array, and use that instead.

    The best solution depends on your situation, of course. But removing array elements in C isn't as simple as it is in, say, Ruby, with something like Array#delete.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right, so the "problem" has several aspects to it. It is correct that we can't delete individual elements in an array. But it is fairly easy to either create second array that contains only the unique elements of the first array, or to remove duplicate elements from an array itself - as suggested, memmove does that sort of thing, but it can be done with a loop as well.

    For any work with arrays, we do need to know how many elements there are in the array (this is not, in C at least, the actual size of the array, as the size may be a "large fixed size" in many cases).

    Once we know that we know the number of elements, we simply write code to work our way through the array (requires two loops, one for "which element we're at" and one for "where we're looking", that goes from the current element to the end of the current number of elements) and eliminate those that are duplicates, shuffling the rest of the array up when there is a duplicate. When the end is reached, all elements should be unique.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    9
    hi;
    thanks for all answers; actually my problem is little bit different ,sorry for the wrong posting.My real problem is ;
    I have txt file which include :

    orange
    green
    white
    orange
    white
    black
    blue
    red
    blue
    white
    green
    green
    orange
    red
    white
    green
    red
    black
    red

    something like that. Now i want to read that txt file then write the other txt file. how can do that ? any idea?

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    The exact same way, except you work with strings instead of numbers.

    Break the data up into an array of strings, step through the array, determining each time if the string has already been entered into your new array. If it hasn't, add it to the array. It it has, skip it.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Sort the array. The duplicates will be right next to each other. Then loop through the array and put every unique value into a second array of the same size and type.

    I use one loop for this. I have *no* idea what looping through an array "for 'where we're looking' ", means.

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    9

    The Code

    I have tried your solution but I can't solve it .(because i have no much more experience in C ). Please Can anyone write code?.. Thanks

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Why don't you post what you have so far - we do not "do" your homework [and whilst this may possibly not be homework, it certainly smells like it - either way, you will learn a whole lot more by doing it yourself].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Apr 2009
    Posts
    9

    where is the wrong part???

    I have started the write code but i doest works properly When i compile the code it can only make unique first element of array. .can anyone say where is the wrong part of my code??


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int strcmp1 (const char * s1, const char * s2)
    {
        for(; *s1 == *s2; ++s1, ++s2)
            if(*s1 == 0)
                return 0;
        return *(unsigned char *)s1 < *(unsigned char *)s2 ? -1 : 1;
    }
    
    int main ()
    {
      
     char *arr[] = {"string1", "string2","string3","string4","string2","string1","string3","string5","string6"};
     char *tmp[sizeof(arr) / sizeof(int)];
     int i,v,j=0;
      char *n=NULL;
       	
          for (i=0;i<sizeof(arr) / sizeof(int);i++){
    	  for (j=i+j;j<sizeof(arr) / sizeof(int);j++){
    		v=strcmp1(arr[i],arr[j]);
    		  if(v != 0){
    			tmp[j]=arr[j];
    		   }   
    		  else{
    			  tmp[j]=" "; 
    		  }
    	
    	    }
    	}
    
        for(i=0;i<sizeof(arr) / sizeof(int);i++){
          printf("%s\n",tmp[i]); 
        }
     
    }

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Your code is setting tmp[j] = arr[j] for every element that isn't matching arr[i].

    Code:
    	  for (j=i+j;j<sizeof(arr) / sizeof(int);j++){
    Also, j = i + j, when you get round to the second iteration of i will be outside the valid size of arr, so the loop will terminate immediately - so once you have gone past i = 0, it will not work. You should use j=i+1 or possibly j = i;

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Apr 2009
    Posts
    9

    solution

    Code:
    #include <stdio.h>
    #include <string.h>
    #define length(a) ( sizeof (a) / sizeof (char *) )
    
     uni ( char **src )
    {
      int  unique = 0; 
      int n=length(src);
      char *dst[length(src)]; 
      int i;
      dst[unique++] = src[0];
    
      for ( i = unique; i < n; i++ ) {
        int has_dup = 0;
        int j;
    
        for ( j = 0; j < unique; j++ ) {
          if ( src[i] == dst[j] )
            has_dup = 1;
        }
    
        if ( has_dup == 0 )
          dst[unique++] = src[i];
      }
    
    
      for ( i = 0; i < unique; i++ )
        printf ( "%s ", dst[i] );
      printf ( " \n  " );
    }
    
    int main( ){
      char *src[]={
      "string1", "string2", "string3",
      "string4", "string2", "string1",
      "string3", "string5", "string6"}; 
      uni(src);
        
    return 0;
    }



    this is the last version of my code.but it needs one more modification.When i compile the this code it produce only first string. I have tried the solve this problem then i find the it. the problem is array length. if give the array length manually it works properly. on the other hand when i give the array length using length global it doest work...any idea?

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You cannot get the size of a passed array using sizeof. It will give you the size of the pointer, not the block of data. Why don't you pass the size as an argument?


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Quote Originally Posted by quzah View Post
    You cannot get the size of a passed array using sizeof. It will give you the size of the pointer, not the block of data. Why don't you pass the size as an argument?
    Another common approach is to throw an empty string or a NULL pointer in at the end of the array. Either way works.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-05-2008, 11:30 PM
  2. Absolute value of each element in a given array
    By DriftinSW20 in forum C Programming
    Replies: 9
    Last Post: 11-15-2007, 04:08 PM
  3. delete an element from a linked list
    By hinman in forum C Programming
    Replies: 6
    Last Post: 10-17-2007, 08:30 PM
  4. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  5. Deleting element from an array problem
    By xamlit in forum C Programming
    Replies: 5
    Last Post: 12-03-2005, 04:53 PM