How can I count the number of unique elements instead of show the unique elements? exp:in input show 4 .Means we have 4 unique | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How can I count the number of unique elements instead of show the unique elements? exp:in input show 4 .Means we have 4 unique

#include<stdio.h> #define max 100 int ifexists(int z[], int u, int v) { int i; for (i=0; i<u;i++) if (z[i]==v) return (1); return (0); } void main() { int p[max], q[max]; int m; int i,k; k=0; printf("Enter length of the array:"); scanf("%d",&m); printf("Enter %d elements of the array\n",m); for(i=0;i<m;i++ ) scanf("%d",&p[i]); q[0]=p[0]; k=1; for (i=1;i<m;i++) { if(!ifexists(q,k,p[i])) { q[k]=p[i]; k++; } } printf("\nThe unique elements in the array are:\n"); for(i = 0;i<k;i++) printf("%d\n",q[i]); }

23rd Dec 2020, 10:25 AM
Pardis
Pardis - avatar
1 Answer
+ 1
Sorry for the late reply! You have already counted the unique number in your code, just have to print it out (value of k)... Moreover i would highly recommend to use more handy variable names.. p, q, m, i, l, k... This is not really nice.. And some more spaces would be good, But this is a matter of opinion :) I have refactored your code a little bit, and for sure added the requested print: https://code.sololearn.com/c5HoR90ltWVx/?ref=app
31st Dec 2020, 4:23 AM
G B
G B - avatar