0
Remove duplicates. What's wrong with my code?
#include <stdio.h> int main() { int n= 0; int count= 0; scanf("%d", &n); int arr[100]; int dupl=0; for(int i=0; i<n; i++) scanf("%d", &arr[i]); for(int i=0; i<n; i++){ for(int j=0; j<=i; j++){ dupl= arr[i]==arr[j] && i!=j; if(dupl==1){ count++; for(int k=i; k<n; k++){ arr[k]= arr[k+1]; } } } } for(int i=0; i<n-count; i++){ printf("%d ", arr[i]); } printf("\ncount of dupl.= %d& new array size= %d", count, n-count); return 0; } https://code.sololearn.com/cf2b202UcYE2/#c
3 Answers
+ 2
What problem you getting with this..?
Edit: Jaan
OK. For some test cases, it failing.
Just change in line 10,
n to n-count in loop like
for(int i=0; i<n-count; i++){
..
}
+ 1
Thank you Jayakrishnađźđł
Great Learning!! Updating the array size in case of deletion/ insertion is important in iterations especially đ
Thank you
0
loop from end