Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- delete_all_duplicate_elements_from_array_v1.c
- Delete duplicate elements in array of n integers.
- https://codeforwin.org/2015/07/c-program-to-delete-duplicate-elements-from-array.html?fbclid=IwAR1YsI0DRAiND3K8qQ3SDUOenu-n48EvRoAKMVXH8CwvA847OcizqMCXBYg
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include <stdio.h>
- #define MAX_SIZE 100 // Maximum size of the array
- // Displays an array of n integers with prompt text
- void display_array_of_integers( char text[], int array[], int n ) {
- int i;
- printf("\n\n%s ( size = %d )\n\n", text, n );
- for( i=0; i<n ; i++ ) // display of array items
- printf("%4d", array[i] );
- printf("\n");
- }
- // Delete duplicate elements in array of n integers
- void delete_all_duplicate_elements_from_array( int arr[], int *n )
- {
- int i, j, k; // Loop control variables
- for(i=0; i<(*n); i++)
- {
- for(j=i+1; j<(*n); j++)
- {
- if(arr[i] == arr[j]) // if any duplicate found
- {
- for(k=j; k<(*n); k++) // delete the current duplicate element
- {
- arr[k] = arr[k + 1];
- }
- (*n)--; // decrement n after removing duplicate element
- j--; // if shifting of elements occur then don't increment j
- } // if(arr[i] == arr[j])
- } // for(j=i+1; j<n; j++)
- } // for(i=0; i<n; i++)
- } // delete_all_duplicate_elements_from_array()
- int main(void)
- {
- int arr[] = {10, 20, 10, 1, 100, 10, 2, 1, 5, 10}; // Declares an array of integers
- int n = sizeof(arr)/sizeof(arr[0]); // A way to get n, number of elements in array arr[]
- display_array_of_integers( "\n Before: ", arr, n );
- delete_all_duplicate_elements_from_array( arr, &n );
- display_array_of_integers( "\n After: ", arr, n );
- return 0;
- } // main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement