OneBite.Dev - Coding blog in a bite size

remove duplicates from an array in C

Code snippet on how to remove duplicates from an array in C

  void removeDuplicate(int arr[], int n)  
  {  
    int i, j, k;  
    for (i = 0; i < n; i++)  
    {  
        for (j = i + 1; j < n; j++)  
        {  
            if (arr[i] == arr[j])  
            {  
                for (k = j; k < n; k++)  
                {  
                    arr[k] = arr[k + 1];  
                }  
                n--;  
                j--;  
            }  
        }  
    }  
  }  

This code is used to remove duplicates from an array in C. It begins by declaring a function called removeDuplicate that takes in an array and its length (n). This function then starts a loop that iterates through each element of the array. For each element (i), there is an inner loop (j) that iterates through the rest of the elements following it. It then checks whether any of these elements match element i. If there is a match, the element is removed, and the elements that come after it are shifted accordingly. The loop then cycles back and the process is repeated until there are no more duplicates.

c