how can i check if the elements in an array exist twice
check
Collapse
X
-
you already opened this thread under "checkNonDuplic ates", and you got the answer there. Why are you asking again? If the answer I (and others ) gave is not fitting, then rephrase your question more specific, and tell me why the aswer is not sufficient or what you did not understand.Originally posted by mia023how can i check if the elements in an array exist twice
Quotation from my answer:
Otherwise, to sort out the duplicates in an array that already contains random generated numbers, you have to compare the last number in array against all the others and delete if it already exists, then the second last against all the others and so on. (like bubble sort).
or you use a Binary Tree for better performance: put the first in, then the second if the second is not already inside, then the third if it is not already inside and so on.
there is also another idea: it's not so fast, but easier to program for you,because it does not need a double-for-loop.
Just convert the number to a string.
start with:
then use a simple for-loop to loop through all the numbers and useCode:Properties properties= new Properties();
.Code:properties.setProperty(currentNumberAsString, "dummyValue")
At the end, you can get all numbers back with "properties.pro pertyNames()".
You will see that there are no duplicates among them. -
Yet another approach to solve this little problem is to start at the other end:
suppose you have a bunch of unique numbers in any particular order such that
the sequence is easy to generate. Next let the Collections.shu ffle() method
do its job on the sequence (a List particularly) and you're ready.
kind regards,
JosComment
Comment