# Delete repeated item entered by user

1

I'm recently watching C programming and we started with the fixes, there is an exercise where I have an N-size array with repeated numbers (entered by the user), what I'm asked to do is that the user enter a number and verify in the fix if it is found, in case it is so that it eliminates the position of the last repeated number (ex: 1 2 3 2 4>> 1 2 3 4), now if it only repeats once it eliminates directly. This last point I get, but when it comes to comparing if there are more repeated ends up doing anything, I send this to a new arrangement that is ours at the end. And well, that is a doubt / problem / situation, if someone were so kind to help me, I appreciate it. Greetings: D

[Edited]

``````int main(){
int arr[n],nuevo[n],i,j,x,nro,cont,band=0;
printf("Ingrese numeros, si son repetidos no importa\n");
for(i=0;i<n;i++){
printf("Ingrese el numero: ");
scanf("%i",&arr[i]);
}
printf("Ingrese un numero a eliminar: ");
scanf("%i",&nro);
for (i=0;i<n;i++) {
cont=0;
if(nro==arr[i]) cont++;
if(cont==1){
x=i;
while(x<n){
arr[x]=arr[x+1];
x++;
}
}
}
for (i=0;i<n-1;i++)   printf ("%i ",arr[i]);
``````

With this code what it does is eliminate the times that with the cont is 1, therefore it will always eliminate the same number to me, and what I want is that it is only the last position.

asked by Edgardo_22 26.05.2017 в 21:32
source

2

The code has a few problems, but referring to the question: the problem is that when you delete a number and run the other elements, the counter goes to the next iteration and skips a number:

problem: 1 2 2 2 3

output: 1 2 2 3

when you check the second two, delete it and move the array, so it stays pointing to the next element, and when you give it to i ++ in the next iteration of the for you jump it

what you could do is [edited]

``````#include <stdio.h>

int main(){
int arr, i, x, nro, cont;
int n = 3;
printf("Ingrese numeros, si son repetidos no importa\n");
for(i=0;i<n;i++){
printf("Ingrese el numero: ");
scanf("%i",&arr[i]);
}
printf("Ingrese el numero para revisar: ");
scanf("%i", &nro);
cont = 0;
for (i = 0; i < n; i++) {
if (nro==arr[i]) {
if (cont) {
x = i;
while(x<n){
arr[x]=arr[x+1];
x++;
}
i--;
n--;
}
cont = 1;
}
}
for (i = 0; i < n; i++) {
printf("%i ", arr[i]);
}
printf("\n");
}
``````