>helos...!!!
>wel am new to dis..!!!
>but i hav a code to it...!!!
>lets try out..!!!!!
You have one of the most annoying writing styles I've seen. If you want people to treat you like anything but an irritating script kiddie, use proper English. The non-native English speakers will thank you.
>try dis out dude..!!!
1) Your code uses a poor style.
2) Your code doesn't work.
Please test solutions before you post them, unless you want someone like me to rip them to shreds, like I'm about to. Let's start with the extremely poor style you're using (I even fixed the indentation for you when I added code tags):
>#include<conio.h>
This is a non-portable header. You'd do well to forget it exists until you're experienced enough to use it wisely, which you most certainly are not right now.
>void main()
There are two standard definitions of main:
int main ( void )
{
return <integer value>;
} and
int main ( int argc, char *argv[] )
{
return <integer value>;
} The portable return values are 0, EXIT_SUCCESS, and EXIT_FAILURE. The latter two are macros defined in stdlib.h.
>int i,j,count=1,flag,arr[5],p[5];
Each variable should be declared on a separate line. This makes it easier to read, document, and maintain your code. It's also a good way to avoid bugs.
>scanf("%d",&arr[i]);
Always check the return values of input functions. If you don't, you have no idea if they succeeded or not, and that makes your code extremely brittle.
>getch();
This is presumably why you included conio.h, and it's a lame reason. getch is a non-portable function and pausing the program is reasonably trivial with standard functions.
Finally, do you realize that you can put whitespace between tokens? It helps readability quite a bit.
>for(j=0;j<=count;j++)
This is where your code is broken. Test these sets of numbers: {1, 2, 2, 2, 2}, {1, 2, 1, 2, 1}. The problem is that you're overrunning the length of the result set by forgetting that any zero-indexed list goes from 0 to N-1, not 0 to N. Use j<count and it'll work better.
Here's my version of your code:
#include <stdio.h>
#define length(a) ( sizeof (a) / sizeof *(a) )
int main ( void )
{
int unique = 0; /* The length of dst after removing duplicates */
int n; /* The length of src after loading numbers */
int src[5]; /* The original set of numbers */
int dst[5]; /* The set of numbers without duplicates */
int i;
/* Load up to N numbers into the source list */
for ( n = 0; n < length ( src ); n++ ) {
if ( scanf ( "%d", &src[n] ) != 1 )
break;
}
/* The first number is never a duplicate */
dst[unique++] = src[0];
/* Load the unique numbers into the destination list */
for ( i = unique; i < n; i++ ) {
int has_dup = 0;
int j;
for ( j = 0; j < unique; j++ ) {
if ( src[i] == dst[j] )
has_dup = 1;
}
if ( has_dup == 0 )
dst[unique++] = src[i];
}
/* Display the unique numbers */
for ( i = 0; i < unique; i++ )
printf ( "%d\t", dst[i] );
printf ( "\n" );
return 0;
}
There's really nothing wrong with your solution, just the implementation bug. However, the flag is a little kludgy when you can use the value of j instead. If j gets to the end of the list, a duplicate wasn't found. This simplifies the operation a smidge:
/* Load the unique numbers into the destination list */
for ( i = unique; i < n; i++ ) {
int j;
for ( j = 0; j < unique; j++ ) {
if ( src[i] == dst[j] )
break;
}
if ( j == unique )
dst[unique++] = src[i];
}