Merging integer array without duplicates.

This is a discussion on Merging integer array without duplicates. within the C Programming forums, part of the General Programming Boards category; Hi all I have to merge two arrays having duplicates into the third array without duplicates. I am trying to ...

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

    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
    20,973
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    54
    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
    20,973
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    54
    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
    20,973
    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().
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 08: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21