Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- are_all_array_elements_different.c
- Are all the elements of the array different from each other.
- Check if all array elements are distinct (unique).
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include <stdio.h>
- void ShowArray( char text[], int array[], int n )
- {
- int i;
- printf("%s", text);
- for(i=0;i<n;i++)
- printf("%2d", array[i]);
- printf("\n");
- }
- // returns 1 if all elements are distinct (unique, different from each other)
- // otherwise returns 0
- int all_array_elements_are_distinct( int arr[], int size )
- {
- int i, j;
- for (i=0; i<size; i++)
- for (j=i+1; j<size; j++)
- if (arr[i] == arr[j])
- return 0;
- return 1;
- }
- int main(void)
- {
- int arr1[10] = {2,5,3,7,4,8,1,0,9,6};
- int arr2[10] = {2,5,3,7,5,8,1,0,9,6};
- int n1 = sizeof(arr1) / sizeof(arr1[0]); // or n1 = sizeof(arr1) / sizeof(int);
- int n2 = sizeof(arr2) / sizeof(arr2[0]); // or n2 = sizeof(arr2) / sizeof(int);
- ShowArray("\n Array arr1[] is: ",arr1,n1);
- if ( all_array_elements_are_distinct(arr1,n1) )
- printf("\n All array elements are distinct (unique, different from each other). \n");
- else
- printf("\n There are repeating elements in the array. \n");
- ShowArray("\n Array arr2[] is: ",arr2,n2);
- if ( all_array_elements_are_distinct(arr2,n2) )
- printf("\n All array elements are distinct (unique, different from each other). \n");
- else
- printf("\n There are repeating elements in the array. \n");
- return 0;
- } // main()
Add Comment
Please, Sign In to add comment