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

26th Jul 2020, 12:37 PM
Tom
Tom - avatar
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++){ .. }
26th Jul 2020, 1:18 PM
Jayakrishna 🇼🇳
+ 1
Thank you Jayakrishna🇼🇳 Great Learning!! Updating the array size in case of deletion/ insertion is important in iterations especially 🙋 Thank you
26th Jul 2020, 2:41 PM
Tom
Tom - avatar
0
loop from end
26th Jul 2020, 4:12 PM
Gordon
Gordon - avatar