Mergring two string Arrays

  • Thread starter Sreedharamurthy K
  • Start date
S

Sreedharamurthy K

Hi,

I've two string arrays A & B. One of it is sorted B. A is not sorted. Now I
want array A to be same as array B. So I was thinking the following is the
right way:

for (elements in B)
check if the element exists in A
if it exists, go to the next element
else add this element

for (elements in A)
check if the element exists in B
if it exists, go to the next element
else remove this element

Is there a better way to do this to reduce the number of string comparion;
doing it faster??

Sorry if it is trivial and has been answered earlier. I searched a little
but could not find any relevant ones...
TIA.
-- Sree
 
S

Sreedharamurthy K

being specific in the logic:

for (elements in B) {
check if the element exists in A
if it exists, go to the next element
else add this element to A
}

for (elements in A) {
check if the element exists in B
if it exists, go to the next element
else remove this element from A
}

-- Sree
 
C

CBFalconer

Sreedharamurthy said:
I've two string arrays A & B. One of it is sorted B. A is not
sorted. Now I want array A to be same as array B. So I was
thinking the following is the right way:

The condition of B doesn't matter. Just copy A into B.
 
M

Malcolm McLean

Sreedharamurthy K said:
Hi,

I've two string arrays A & B. One of it is sorted B. A is not sorted. Now
I want array A to be same as array B. So I was thinking the following is
the right way:

for (elements in B)
check if the element exists in A
if it exists, go to the next element
else add this element

for (elements in A)
check if the element exists in B
if it exists, go to the next element
else remove this element

Is there a better way to do this to reduce the number of string comparion;
doing it faster??

Sorry if it is trivial and has been answered earlier. I searched a little
but could not find any relevant ones...
TIA.
B is sorted, so call bsearch to find if each element in A exists in B, in
O(log N) time. Then if you find the element, flag it (there may be
complications if you allow duplicate elements). Then any unflagged elements
remaining in B do not exist in A.
 
E

Eric Sosman

Sreedharamurthy K wrote On 08/02/07 16:56,:
Hi,

I've two string arrays A & B. One of it is sorted B. A is not sorted. Now I
want array A to be same as array B. So I was thinking the following is the
right way:

for (elements in B)
check if the element exists in A
if it exists, go to the next element
else add this element

for (elements in A)
check if the element exists in B
if it exists, go to the next element
else remove this element

Is there a better way to do this to reduce the number of string comparion;
doing it faster??

Sorry if it is trivial and has been answered earlier. I searched a little
but could not find any relevant ones...

Throw the old contents of `A' away and copy `B' to `A'.
 
S

Sreedharamurthy K

Thx. Easiest, I know... :) That would be my last resort... I wanted to know
if I could get it without have to recreate the entire array A from B.
 
E

Eric Sosman

Sreedharamurthy K wrote:

(.ereh dexiF) !GNITSOP-POT POTS ESAELP
> Thx. Easiest, I know... :) That would be my last resort... I wanted to know
> if I could get it without have to recreate the entire array A from B.

Instead of a single O(|B|) call to memcpy(), you prefer
to indulge in an O(|B| * |A|) procedure? That's like buying
a bigger car so you can carry home more lottery tickets.
 
M

Malcolm McLean

Eric Sosman said:
Sreedharamurthy K wrote:

(.ereh dexiF) !GNITSOP-POT POTS ESAELP



Instead of a single O(|B|) call to memcpy(), you prefer
to indulge in an O(|B| * |A|) procedure? That's like buying
a bigger car so you can carry home more lottery tickets.
You were right.
I had imagined that the the order in A would need to be preserved or
something, but in fact it is just a copy job.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top