Finding Duplicate Elements

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Suyash Upadhyay
    New Member
    • Mar 2007
    • 34

    Finding Duplicate Elements

    How to find more than one duplicate elements in array? ( One method is BST, but I want efficient method than it)
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You may have to do a progressive loop where you start with element 0 and traverse the array looking for dupluicates. Then start with element 1, etc.

    If duplicates is an error condition, and you are in C++, then use a set<> container, which does not allow duplicates. At run time if the insert fails, the new value is a duplicate. Of course, set<> uses a red/black tree, but there you are.

    Comment

    • Suyash Upadhyay
      New Member
      • Mar 2007
      • 34

      #3
      Originally posted by weaknessforcats
      You may have to do a progressive loop where you start with element 0 and traverse the array looking for dupluicates. Then start with element 1, etc.

      If duplicates is an error condition, and you are in C++, then use a set<> container, which does not allow duplicates. At run time if the insert fails, the new value is a duplicate. Of course, set<> uses a red/black tree, but there you are.
      I am aware of set<> but I want to search algorithm better than O(n^2) and algorithm proposed is O(n^2). Cann't we make it better?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        You might need a hash chain-link table.

        That is, an array of linked lists. At least you would only have to traverse the list of elements that had the same hash value.

        I think Robert Sedgewick ("Algorithms in C++") calls this separate chaining. It's been a long time.

        Comment

        Working...