Thread: Merging integer array without duplicates.

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    103

    Merging integer array without duplicates.

    Hi all

    I have to merge two arrays having duplicates into the third array without duplicates.

    I am trying to implement this by binary tree implementation in which if there is repeated element it will return.

    I want simple merging with less loops or iteration( sorting is not required so I skip merge sort method).

    Can anybody suggest me some methods with least looping. Currently I am going with Binary tree but if anybody has much better idea, it will be very helpful.

    example:
    Code:
    int a[]= { 11,12,13,14,15};
    int b[]= {11,12,45,32,13,55,67,34};
    
    int final_arr[]= {11,12,13,14,45.32.67.34.15.55};
    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Using a balanced binary tree to implement a set is certainly a correct idea. However, you may find it easier to simply concatenate the two arrays, sort the result, and then eliminate consecutive duplicates. Or, you can sort each of the input arrays, then perform a merge that skips duplicates.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    103
    Thanks for reply...

    I have some queries for merging:

    1) how would be I'll knowing the size of final array to be created. Or should I preassumes to allocate a size equal to some of a & b array. In this I'll be ended by extra memory wastage.

    Thanks

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You first allocate enough space on the assumption that there are no duplicates. After all is done, you can always realloc() to the desired size.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    103
    Thanks...

    I have wrote following code and it merging but giving segmentation fault at last.

    I have taken third array as:
    Code:
    int c[] = {};
    As think that declaring array c like this, we can add any number of elements to it.

    Please clarify and help me to correct problem.

    Thanks

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Err... I am not sure about that. A quick check with the MinGW port of gcc 3.4.5 shows that the empty initialiser is a compiler extension, even with respect to C99. I have no motivation to check the standard right now, so I'll just tell you that the typical approach here is to use dynamic memory allocation with functions from <stdlib.h> such as malloc(), realloc() and free().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert Integer to Hex in an array of chars
    By anasimtiaz in forum C Programming
    Replies: 12
    Last Post: 07-22-2009, 05:29 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM