How i can find duplicated numbers in an array.
#1.I have a rand() function that assign numbers in an array.
#2.I want to find if there are at least 2 duplicated numbers,
unfortunately i dont know how ... can you give me your suggestions plz...
How i can find duplicated numbers in an array.
#1.I have a rand() function that assign numbers in an array.
#2.I want to find if there are at least 2 duplicated numbers,
unfortunately i dont know how ... can you give me your suggestions plz...
Just check each number in the array against the others...so it'd be like:Originally Posted by dionys
I know this doesn't work, and there is flaws in it, but it's a general idea.Code:i = 1; t = 20; while i < 20 if numbers_in_array[i] = numbers_in_array[i++] do something
Last edited by Quantrizi; 04-11-2004 at 05:08 PM. Reason: added warning
>can you give me your suggestions plz...
Use a frequency table would be my first suggestion:
Code:#include <stdio.h> int main ( void ) { int a[] = {0,1,2,3,4,5,4,6,7,3,8,9}; int freq[10] = {0}; int i; for ( i = 0; i < 12; i++ ) freq[ a[i] ]++; for ( i = 0; i < 10; i++ ) printf ( "%d: %d\n", i, freq[i] ); return 0; }
My best code is written with the delete key.
2 nested loops, try comparing each array element with every other array element. give it a shot, post code, and the highly secret, arcane knowledge required to do this (which cannot be found anywhere else on the internet!) will be passed on to you, young padawan
edit: o shi
.sect signature
Prelude's idea of using a frequency table is definitely more efficient than checking every element in an array in most cases, although if you're choosing 10 random numbers from a group of 400 000, the check every element method works much better (in terms of memory efficiency).
Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah
You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie
>although if you're choosing 10 random numbers from a group of 400 000
Of course, since the OP gave no specific numbers, we can't make that assumption. So without more detail, the simpler method is preferred. If the range of numbers is too much for a table, then a binary search tree would be much better than a table for space usage, without much loss in performance. However, the nested loop option will likely be more efficient if the arrays are sure to be very small. The choice of solution depends on the data.
My best code is written with the delete key.
Or the guaranteed method is preferred, as a freq table may be too large and unwieldly...Originally Posted by Prelude
So trueOriginally Posted by Prelude
Definition: Politics -- Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers