c - Cant get the logic of this code -


how code work input : 20051996

program: delete duplication elements in array

    for(i=0;i<n;i++)     {       for(j=i+1;j<n;j++)        {            if (a[i]==a[j])           {              for(k=j;k<n;k++)              {                a[k]=a[k+1];              }             n--;             j--;           }        }     } 

the logic pretty simple here element not deleted being exchanged.

for(i=0;i<n;i++) {   for(j=i+1;j<n;j++)    {        if (a[i]==a[j]) 

this finding element equal a [i]. number there loop targeting repeated number , cell fed/replaced next cell value. 1 value has been changed remaining cell value swapped next one.

for(k=j;k<n;k++)          {            a[k]=a[k+1];          }         n--;         j--; 

and last cell remains , number n , j decreased not refer cell again. removes element makes new element have put counter number of repetition , have slice array or break make work.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -