473,385 Members | 1,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Mergring two string Arrays

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
Aug 2 '07 #1
7 1273
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

"Sreedharamurthy K" <ks***@hpus.comwrote in message
news:f8**********@usenet01.boi.hp.com...
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

Aug 2 '07 #2

"Sreedharamurthy K" <ks***@hpus.comwrote in message
news:f8**********@usenet01.boi.hp.com...
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.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Aug 2 '07 #3
Thanks. I guess this will definitely improve!! :)

"Malcolm McLean" <re*******@btinternet.comwrote in message
news:yv******************************@bt.com...
>
"Sreedharamurthy K" <ks***@hpus.comwrote in message
news:f8**********@usenet01.boi.hp.com...
>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.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Aug 2 '07 #4
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'.

--
Er*********@sun.com
Aug 2 '07 #5
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.
"Eric Sosman" <Er*********@sun.comwrote in message
news:1186091646.74954@news1nwk...
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'.

--
Er*********@sun.com

Aug 2 '07 #6
Sreedharamurthy K wrote:
>
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.

--
"Vista is finally secure from hacking. No one is going to 'hack'
the product activation and try and steal the o/s. Anyone smart
enough to do so is also smart enough not to want to bother."
--
Posted via a free Usenet account from http://www.teranews.com

Aug 3 '07 #7
Sreedharamurthy K wrote:

(.ereh dexiF) !GNITSOP-POT POTS ESAELP
"Eric Sosman" <Er*********@sun.comwrote in message
news:1186091646.74954@news1nwk...
>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'.
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.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Aug 3 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Java script Dude | last post by:
Some programmers prefer to stay with native level data structures such as string arrays instead of using Object based data structures such as ArrayList. From and efficiency point of view. Are...
16
by: Don Starr | last post by:
When applied to a string literal, is the sizeof operator supposed to return the size of the string (including nul), or the size of a pointer? For example, assuming a char is 1 byte and a char *...
4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
17
by: Chad Myers | last post by:
I've been perf testing an application of mine and I've noticed that there are a lot (and I mean A LOT -- megabytes and megabytes of 'em) System.String instances being created. I've done some...
37
by: jortizclaver | last post by:
Hi, I'm about to develop a new framework for my corporative applications and my first decision point is what kind of strings to use: std::string or classical C char*. Performance in my system...
18
by: greenflame | last post by:
I want to make a function that does the following. I will call it thefunc for short. '||Char>>' I tried the following def thefunc(s): s = "||" + s + ">>"
14
by: rohitpatel9999 | last post by:
Hi While developing any software, developer need to think about it's possible enhancement for international usage and considering UNICODE. I have read many nice articles/items in advanced C++...
3
by: SneakyElf | last post by:
i am very green with c++ so i get stuck on very simple things anyway, i need to write a program that would read data from file (containing names of tv shows and their networks) one line at a time...
8
by: Spoon | last post by:
Hello, Could someone explain why the following code is illegal? (I'm trying to use a list of (C-style) arrays.) #include <list> typedef std::list < int foo_t; int main() { int v = { 12, 34...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.