Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Duplicate elements in 2 Arraylist

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    114
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Duplicate elements in 2 Arraylist

    Hi,

    Given 2 arraylists containing string objects of size one million each. Have to find algorythm to find duplicate string objects between 2 array lists.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Duplicate elements in 2 Arraylist

    Concatenate the two arrays together and try to build a heap. If there's a collision on the comparison of any two strings while building the heap, you know that two strings are lexicographically equal. You can throw out one of the duplicates and continue building the heap to find all duplicates (note that you'll have to take into account the fact that the heap size just decreased), or keep both and use an external list to keep track of duplicates. Alternatively, try to do a merge sort of the two arrays. If you run into two strings being equal during the merge portion, you know there's a duplicate. Remove one of them and continue.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Duplicate elements in 2 Arraylist

    A Collection object contains methods that allow one to find unions, intersections, etc... between two (or more) Collection objects. For example, to find Objects identical between two Collection's (in your case Lists), create a new list, add all items from the first list to this object, then call retainAll(), passing the second list. The result is a list containing items that are found in both.

  4. The Following User Says Thank You to copeg For This Useful Post:

    tcstcs (April 18th, 2011)

  5. #4
    Member
    Join Date
    Mar 2011
    Posts
    114
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Duplicate elements in 2 Arraylist

    Thanks Copeg.. I tried with ur idea.. it works perfectly fine.. Thanks a lot..

Similar Threads

  1. How would you check for duplicate usernames in a file?
    By suxen in forum Java Theory & Questions
    Replies: 1
    Last Post: March 28th, 2011, 11:35 PM
  2. check duplicate key and value in hashmap
    By starmandell in forum Algorithms & Recursion
    Replies: 1
    Last Post: February 9th, 2011, 04:25 PM
  3. duplicate t:dataTable
    By smackdown90 in forum Web Frameworks
    Replies: 0
    Last Post: August 4th, 2010, 10:35 AM
  4. accessing elements in a 2d ArrayList
    By Flamespewer in forum Collections and Generics
    Replies: 2
    Last Post: March 8th, 2010, 06:11 PM
  5. Eliminating duplicate values
    By Harry_ in forum Collections and Generics
    Replies: 7
    Last Post: November 9th, 2009, 06:35 AM