Removing duplicates from an array using C

I want a little clarification on the concept of an array in C. I have an array:

int a[11]={1,2,3,4,5,11,11,11,11,16,16}; 

I want to get the result as follows:

 {1,2,3,4,5,11,16} 

So I want to remove duplicates. How is this possible?

+4
source share
9 answers

You cannot easily resize arrays to C - at least not arrays as you declared it. Obviously, if the data is in an ordered order, direct copying the data to the front of the selected array and handling it as if it had the correct smaller size (and this is the linear O (n) algorithm), if the data is not sorted, they become messy; the trivial algorithm is quadratic, so perhaps sorting (O (N lg N)) is best for it, followed by a linear algorithm.

You can use dynamically allocated memory to manage arrays. However, this may be outside the place where you came to your research.

 #include <assert.h> #include <stdio.h> #include <stdlib.h> static int intcmp(const void *pa, const void *pb) { int a = *(int *)pa; int b = *(int *)pb; if (a > b) return +1; else if (a < b) return -1; else return 0; } static int compact(int *array, int size) { int i; int last = 0; assert(size >= 0); if (size <= 0) return size; for (i = 1; i < size; i++) { if (array[i] != array[last]) array[++last] = array[i]; } return(last + 1); } static void print(int *array, int size, const char *tag, const char *name) { int i; printf("%s\n", tag); for (i = 0; i < size; i++) printf("%s[%d] = %d\n", name, i, array[i]); } int main(void) { int a[11] = {1,2,3,4,5,11,11,11,11,16,16}; int a_size = sizeof(a) / sizeof(a[0]); print(a, a_size, "Before", "a"); a_size = compact(a, a_size); print(a, a_size, "After", "a"); int b[11] = {11,1,11,3,16,2,5,11,4,11,16}; int b_size = sizeof(b) / sizeof(b[0]); print(b, b_size, "Before", "b"); qsort(b, b_size, sizeof(b[0]), intcmp); print(b, b_size, "Sorted", "b"); b_size = compact(b, b_size); print(b, b_size, "After", "b"); return 0; } 
+9
source
 #define arraysize(x) (sizeof(x) / sizeof(x[0])) // put this before main int main() { bool duplicate = false; int a[11] = {1,2,3,4,5,11,11,11,11,16,16}; // doesnt have to be sorted int b[11]; int index = 0; for(int i = 0; i < arraysize(a); i++) { // looping through the main array for(int j = 0; j < index; j++) { // looping through the target array where we know we have data. if we haven't found anything yet, this wont loop if(a[i] == b[j]) { // if the target array contains the object, no need to continue further. duplicate = true; break; // break from this loop } } if(!duplicate) { // if our value wasn't found in 'b' we will add this non-dublicate at index b[index] = a[i]; index++; } duplicate = false; // restart } // optional int c[index]; // index will be the number of objects we have in b for(int k = 0; k < index; k++) { c[k] = b[k]; } } 

If you really need to, you can create a new array where it is the right size and copy it into it.

As you can see, C is a very simple (but powerful) language, and if you can, use a vector instead, but instead (C ++ std :: vector), which can easily grow to suit your needs.

But while you use only a small number of integers, you should not lose much. If you have a lot of data, you can always allocate an array on the heap using "malloc ()" and choose a smaller size (maybe half the size of the original source array), which you can then increase (using realloc ()), since you add more objects to it. There are some flaws that reallocate memory all the time, but is the solution you need to make fast, but allocate more data than you need? or slower and with the exact number of elements you need to allocate (which you really can't control, since malloc () can allocate more data than you need in some cases).

+1
source
 //gcc -Wall q2.cc -o q2 && q2 //Write a program to remove duplicates from a sorted array. /* The basic idea of our algorithm is to compare 2 adjacent values and determine if they are the same. If they are not the same and we weren't already looking previusly at adjacent pairs that were the same, then we output the value at the current index. The algorithm does everything in-place and doesn't allocate any new memory. It outputs the unique values into the input array. */ #include <stdio.h> #include <assert.h> int remove_dups(int *arr, int n) { int idx = 0, odx = -1; bool dup = false; while (idx < n) { if (arr[idx] != arr[idx+1]) { if (dup) dup = false; else { arr[++odx] = arr[idx]; } } else dup = true; idx++; } return (odx == -1) ? -1 : ++odx; } int main(int argc, char *argv[]) { int a[] = {31,44,44,67,67,99,99,100,101}; int k = remove_dups(a,9); assert(k == 3); for (int i = 0;i<k;i++) printf("%d ",a[i]); printf("\n\n"); int b[] = {-5,-3,-2,-2,-2,-2,1,3,5,5,18,18}; k = remove_dups(b,12); assert(k == 4); for (int i = 0;i<k;i++) printf("%d ",b[i]); printf("\n\n"); int c[] = {1,2,3,4,5,6,7,8,9}; k = remove_dups(c,9); assert(k == 9); for (int i = 0;i<k;i++) printf("%d ",c[i]); return 0; } 
+1
source

you should create a new array, and you should check the array if it contains the element you want to insert before inserting a new element into it.

0
source

The question is not clear. Although, if you are trying to remove duplicates, you can use nested for loops and delete all those values ​​that occur more than once.

0
source

C does not have a built-in data type that supports what you want - you will need to create your own.

0
source

int a [11] = {1,2,3,4,5,11,11,11,11,16,16};

Since this array is a sorted array , you can achieve very easily by following the code.

 int LengthofArray = 11; //First elemnt can not be a duplicate so exclude the same and start from i = 1 than 0. for(int i = 1; i < LengthofArray; i++); { if(a[i] == a[i-1]) RemoveArrayElementatIndex(i); } //function is used to remove the elements in the same as index passed to remove. RemoveArrayElementatIndex(int i) { int k = 0; if(i <=0) return; k = i; int j =1; // variable is used to next item(offset) in the array from k. //Move the next items to the array //if its last item then the length of the array is updated directly, eg. incase i = 10. while((k+j) < LengthofArray) { if(a[k] == a[k+j]) { //increment only j , as another duplicate in this array j = j +1 ; } else { a[k] = a[k+j]; //increment only k , as offset remains same k = k + 1; } } //set the new length of the array . LengthofArray = k; } 
0
source

You can use qsort from stdlib.h to make sure your array is sorted in ascending order to remove the need for a nested loop.

Note that qsort requires a function pointer (int_cmp in this case), I have included it below.

This int_array_unique function returns a duplicated free array "in place", that is, overwrites the original and returns the length of the duplicated free array using the pn pointer

 /** * Return unique version of int array (duplicates removed) */ int int_array_unique(int *array, size_t *pn) { size_t n = *pn; /* return err code 1 if a zero length array is passed in */ if (n == 0) return 1; int i; /* count the no. of unique array values */ int c=0; /* sort input array so any duplicate values will be positioned next to each * other */ qsort(array, n, sizeof(int), int_cmp); /* size of the unique array is unknown at this point, but the output array * can be no larger than the input array. Note, the correct length of the * data is returned via pn */ int *tmp_array = calloc(n, sizeof(int)); tmp_array[c] = array[0]; c++; for (i=1; i<n; i++) { /* true if consecutive values are not equal */ if ( array[i] != array[i-1]) { tmp_array[c] = array[i]; c++; } } memmove(array, tmp_array, n*sizeof(int)); free(tmp_array); /* set return parameter to length of data (eg no. of valid integers not * actual allocated array length) of the uniqe array */ *pn = c; return 0; } /* qsort int comparison function */ int int_cmp(const void *a, const void *b) { const int *ia = (const int *)a; // casting pointer types const int *ib = (const int *)b; /* integer comparison: returns negative if b > a and positive if a > b */ return *ia - *ib; } 
0
source

Save an array element with a small condition to a new array ** just starts when 100% will work!) Save the first value in the array

Ii) save the check of another item before the saved value.

Iii) if it exists, leave the element - and check the next and save

here the code below works and you will understand better

 int main() { int a[10],b[10],i,n,j=0,pos=0; printf("\n enter an value "); scanf("%d",&n); printf("\n enter a array value"); for(i=0;i<n;i++) { scanf("%d",&a[i]);//gets the arry value } for(i=0;i<n;i++) { if(check(a[i],pos,b)==0)//checks array each value its exits or not { b[j]=a[i]; j++; pos++;//count the size of new storing element } } printf("\n after updating array"); for(j=0;j<pos;j++) { printf("\n %d",b[j]); } return 0; } int check(int x,int pos,int b[]) { int m=0,i; for(i=0;i<pos;i++)//checking the already only stored element { if(b[i]==x) { m++; //already exists increment the m value } } return m; } 
0
source

Source: https://habr.com/ru/post/1400340/


All Articles