Question:
Determines whether all the elements in a given
array are distinct
Example:
Input: arr[]={1,2,3,4,5,3}
Output: Repeated Element Exists
Solution:
Approach:
ALGORITHM UniqueElements(A[0..n - 1])
//Determines whether all the elements in a given array are distinct
//Input: An array A[0..n - 1]
//Output: Returns “true” if all the elements in A are distinct
// and “false” otherwise
for i ← 0 to n - 2 do
for j ← i + 1 to n - 1 do
if A[i] = A[j] return false
return true
C program:
#include <stdio.h>
int main(){
int array[100],d, c, n,flag=0;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d integer(s)\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0; c < n; c++){
for(d=c+1;d<n;d++) {
if (array[c] ==array[d]) /* If required element is found */ {
flag=1;
printf("%d is present at location %d and %d.\n",array[d], c+1,d+1);
break; }
}
}
if (flag==0){ printf("No repeated element present in the array.\n");
}
if (flag==1){
printf("Repeated element present in the array.\n");
}
return 0;
}
Output:
Enter number of elements in array
0 comments:
Post a Comment