good day to you guys... my first time here...
i just would like to ask if
given the series of numbers 8 1 5 9 4 5
how can i create a program in C to print to screen that there is a duplicate number which is 5...
thanks
This is a discussion on duplicate numbers within the C Programming forums, part of the General Programming Boards category; good day to you guys... my first time here... i just would like to ask if given the series of ...
good day to you guys... my first time here...
i just would like to ask if
given the series of numbers 8 1 5 9 4 5
how can i create a program in C to print to screen that there is a duplicate number which is 5...
thanks
How do you yourself find if there are duplicate numbers? Well, that's pretty easy. You start with the first number, and look in the whole list for it. Then you pick the next, and so on. Give it a shot.
Quzah.
Hope is the first step on the road to disappointment.
If the list tends to be large
you should sort the list
and then loop through, checking if successive numbers are equal.
Actually what is better than a sort is something like so...
Code:/* Pseudocode... */ Create an array the size of the frequency of numbers For each number in the list add one to the spot on the frequency array Print the index of each spot with the number 2Code:// list contains numbers from 1 to 100 // the frequency array will need to have each number the list has so we must make // the last position 100 so we can use 1 instead of 0 int frequency[100+1] = { 0 }; int size = sizeof (list); for (int loop = 0; loop < size; loop++) { frequency[ list[loop] ]++; } for (int index = 1; index <= 100; index++) { if (frequency[index] == 2) printf("Duplicate number in list: %d", index); }
Last edited by JoshR; 07-15-2005 at 05:27 PM.
i did it... thank you very much...
this looks more correct:
Code:for (i =0; i < sizeof(list) / sizeof(list[0]); i++) if (list[i]++) printf("duplicate: %d", i);
Of course, if the range is small, this method is OK. But if the range is big, for example,
there could be 32-bit numbers, it will not work: the 'list' array gonna take too much memory.
In this case, other methods should be used, so numbers must be organized in another
data structure instead of plain array: linked list, tree, or hash.
Hashes are particularly useful for checking the uniquesness.
Quick introduction to hashes can be found at http://silversoft.net/hash.txt