It would be more efficient to test the element of the first array by comparing it to the element of the second to avoid adding it twice. One can also use an index to vary the size of the array base on how much overlap exists between the two sets.
Can anybody pls giv me the C program to find the union of two sets of numbers (placed in two arrays?
I gave up C a long time ago, but generally, create an array big enough for all the numbers. This will hold the answer.
answerPos = 0
Loop through numbers array one...
-- For each number, add it to the answer array...
---- answer[answerPos] = number
---- answerPos++
Loop through numbers array two...
-- For each number, loop through the big answer array...
---- If the number's not in the answer array, add it:
------ answer[answerPos] = number
------ answerPos++
The code's pretty inefficient, you might want to deal with all the empty memory at the end of the answers array too.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment