I have two arrays. Some elements are duplicated, it really depends on data length at runtime. I want to merge the arrays.
Wondering, can anybody suggest me best method to merge the arrays.
First algo comes to my mind is,
1. Sort one of the array.
2. Binary-search each element of second array in sorted array.
3. Place non-duplicated element in third array.
4. Copy all the elements of sorted array into third array after all the elements in second array have sorted.
1. Array1 and array 2 are of same size. If number of elements are less than actual array length then, remaining elements will be zeroed.
2. I will allocated third element double the size of first two arrays.
Disadvatage with this is, we need to allocate third array.
Second algorithm can be,
Create binary tree of longer array. Then keep on adding elements of second array. Drop adding to tree if element duplicated otherwise add to tree.
If you have better and efficient algo. Please let me know..