I have same question as posted by holla and Iam not sure about merging the contents of 2 sorted arrays into another array without duplication of values.
two arrays merging into new array minus duplicates
Collapse
X
-
-
Question is write a function that will merge the contents of two sorted arrays of type double values, sorting the result in an array output of parameter (still in ascending order). This function should not assume that both its input parameters are the same length but can assume that one array does not contain two copies of the same value. The result array should not contain duplicate values.Comment
-
Yes, I understand the question that is your assignment but you haven't said exactly where you are stuck.Comment
-
Ignore following javascript solution.
I did not realize I was posting to the wrong forum.
Sorry :(
This does not remove duplicates since you are not stuck on that part.
The correct command is concat()
Here are two examples.
Code:<script type="text/javascript"> var arr1 = [1,2,3,4,5]; var arr2 = ['a','b','c','d','e','f','g','h','i','j']; var arrs = [].concat(arr1,arr2); alert(arrs.join('\n')); alert(arr2.concat(arr1).join('\n')); </script>
Comment
-
@jmrker
This is the C/C++ forum. Not Javascript.
@perogy:
No it's not strncpy. I suspect that you don't know the steps required to do a merge.
So If you merge array A,say 10 elements, and array B, say 20 elements, into array C you would need to:
1) sort A
2 sort B
3) allocate C for 30 elements (incase there are n duplicates)
4) copy either A or B to C
5) now the merge:
is A[0] in C? If yes, do nothing. If no, add A[0] to the end of C
6) repeat step 5 for all elements of A
7 sort C
8 done.
You can never code until you know what to code. So writng a small outline of the logic is important. Then you code the outline.
Post again with your code.Comment
-
Thanks all of you for giving me the idea. The second answer is what was on my mind but I wasnt sure about the merge without the duplicates. Now I know that I can check each element of array c with the element in array a first and then b and then copy them into c.Comment
-
You are directed to eliminate duplicate values when you merge the two arrays. That would be straightforward for merging arrays of integers but you are working with arrays of doubles. It is usually bad form to test floating point numbers for equality. Read what every computer scientist should know about floating-point arithmetic for a very detailed explanation.Comment
Comment