check

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mia023
    New Member
    • May 2007
    • 89

    #1

    check

    how can i check if the elements in an array exist twice
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    Originally posted by mia023
    how can i check if the elements in an array exist twice
    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.

    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:
    Code:
    Properties properties= new Properties();
    then use a simple for-loop to loop through all the numbers and use
    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.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      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,

      Jos

      Comment

      Working...