tags:

views:

101

answers:

4

HI , In Java Script ,

var a ="apple-orange-mango" var b ="grapes-cheery-apple"

var c = a + b // Merging with 2 variable

var c should have value is "apple-orange-mango-grapes-cheery" .Duplicated should be removed.

Thanks , Chells

+2  A: 

After your string is combined, you will want to split it using the delimiters (you can add these back in later).

example:

var a ="apple-orange-mango" 
var b ="grapes-cheery-apple"
var c = a + "-" + b
var Splitted = c.split("-");

the Splitted variable now contains an array such as [apples,orange,mango,grapes,cherry,apple]

you can then use one of many duplicate removing algorithms to remove the duplicates. Then you can simply do this to add your delimiters back in:

result = Splitted.join("-");
John T
c should be a + "-" + b
Eineki
you'd need to include an additional "-" when concatenating between the strings in this particular example.eg<code>var a ="apple-orange-mango" var b ="grapes-cheery-apple"var c = a + "-" + b</code>
fearoffours
@both of you....that is copied from OP's code, moreso their mistake. But good eye. Fixed.
John T
+1  A: 

I don't know if it is an homework.

By the way you can split strings like a and b with the split method of string object. in your case:

firstArray=a.split("-");
secondArray=b.split("-");

the removal of duplicates is up to you...

Eineki
+1  A: 

In your simple example, just use var c = a + "-" + b; If you want duplicates removed, split a and b into arrays, and combine them, like so:

var avalues = a.split("-");
var bvalues = b.split("-");
var combined = avalues.concat( bvalues );
// now loop over combined and remove duplicates
JayVee
+1  A: 

Here's a brute force algorithm:

var a;
var b; // inputs

var words = split(a+b);
var map = {};
var output;
for( index in words ) {
    if( map[ words[index] ]!=undefined ) continue;
    map[ words[index] ] = true;
    output += (words[index] + '-');
}
output[output.length-1]=' '; // remove the last '-'

The 'map' acts as a hashtable.

Thats it!

Cheers, jrh

Here Be Wolves
This Wroks .. But We need to change the code some what
joe
<script language="javascript" type="text/javascript">var a = "s-d-g";var b = "c-d-s";var c = a + "-" + bvar Splitted = c.split("-");var words = Splittedvar map = {};var output="";for( index in words ) { if( map[ words[index] ]!=undefined ) continue; map[ words[index] ] = true; if ( output == "") { output = words[index]; } else { output += ('-'+words[index]); } }output[output.length-1]=' '; alert(output) ;</script>
joe
Shouldn't we be encouraging people to learn instead of purely posting the answer, especially to such a simple problem.
gonzohunter
@gonzohunter You are right, but this forum here is for programming, not encouragement. So I justify my actions :)
Here Be Wolves
I just slove some of other issues of using this . It not like simple problem . Its seriouly i need to for some cirtical systems
joe