I want to delete duplicates values in array. For example: array[1,5,6,1,3,5,9] I want to have array[6,3,9].
I have written this, but I am having troubles:
#include<stdio.h>
main() {
int array[50], i, j, k=0, c=0, array2[50], n;
printf("Enter array dimension: "); scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("array[%d]= ", i); scanf("%d", &array[i]);
}
for (i = 0; i < n; ) {
for (j = i + 1; j < n; j++) {
if (array[i] == array[j])
i++;
else {
array2[k++] = array[i];
c++;
}
}
}
for (k = 0; k < c; k++) {
printf("%d ", array2[k]);
}
system("pause");
}
iin the firstforloop. Ifarray[i] == array[j]doesn't happen,iwill stay the same forever. Also,kandcseem to have the same value, get rid of one of them. Is the range of numbers limited? Do you have any limits on performance (big O), or isO(n*n)acceptable?iin all cases. Try to debug (step by step, using your favorite debugger) the case when input array is[1,2,3].main()?? Seriously,mainwithout arguments has been deprecated/obsolete/bad practice since sometime in the 90s. Are people still using early K&R, or reading C++ books, or what? This is 2014. In C, the main function either takes two argument, orvoid. It is not a variadic function!