×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

How to find duplicates in two arrays

How to find duplicates in two arrays

How to find duplicates in two arrays

(OP)
I have two arrays that have overlapping elements. I would like to make a third array that only has the unique elements.

I can concatenate them using Array1.concat(Array2), but this of course just puts them together. I suspect the answer is finding the duplicates in this array and zapping them.

But how do I do that?

Help is appreciated,
Tom

RE: How to find duplicates in two arrays

Another approach would be to concatentate the two arrays, sort the new array, then delete all items that have duplicates.

There are a couple other ways to go at this problem, but Dan's would probably be what I tried first, though it will be slow if the arrays are real large.

Lee

RE: How to find duplicates in two arrays

If the arrays are large, then the sorting part would be slow regardless.

What I would suggest is to concatenate the arrays, and loop through them putting unique values into a string and then use the split to break the string into your third array.

Something like this:

CODE

var strUnique = "";
Array1.concat(Array2);
for(inx=0;inx<Array1.length;inx++) {
  if (strUnique.indexOf(Array1[inx]) < 0) {
    strUnique += ","+Array1[inx];
  }
}
Array3 = Array1.split(",");

  Einstein47 (Starbase47.com)
“Never put both feet in your mouth at the same time.
Because then you won't have a leg to stand on.“

 - Unknown

RE: How to find duplicates in two arrays

That's a good idea, but it wouldn't work as written if any elements that were strings were contained in longer strings.

I'd like to see what the original poster has rather than provide a free coding service for him.  This could well be a homework assignment.

Lee

RE: How to find duplicates in two arrays

(OP)
Here is what I wrote. It adds all the elements in Array1 and Array2, creating Array3. It sorts the latter so that all duplicate elements are next to each other, finds the duplicates and converts them to nothing.

But, Array3 still has a total of 12 elements. You can see this in the alert because there are a bunch of commas. I haven't figured out how to return only the unique elements in the first two arrays, i.e., get rid of all those commas.

Any suggestions?

Tom



myArray1 = new Array("tanks","Banks", "Thanks", "men", "women", "gals")
myArray2 = new Array("men","tanks", "guys","Banks", "women", "gals")
myArray3 = myArray1.concat(myArray2);
myArray3.sort();
for (var i = 0; i<myArray3.length; i++ ){
    if (myArray3[i] == myArray3[i+1]){
        myArray3[i] = "";
        myArray3[i+1] = "";
    }
     }
window.alert(myArray3)

RE: How to find duplicates in two arrays

You're going to get an error unless you shorten your for loop upper limit by 1

CODE

for (var i = 0; i<myArray3.length - 1; i++ ){

You're seeing the commas because that's what JS uses as separators for the array elements when you display the whole array at once.

Here's something that might work for you (and this will need some help from the Reg Exp guys):

CODE

var myArray1 = new Array("tanks","Banks", "Thanks", "men", "women", "gals")
var myArray2 = new Array("men","tanks", "guys","Banks", "women", "gals")
var arrstring = '~' + myArray1.join('~') + '~' + myArray2.join('~');

for (var mi = 0; mi < myArray1.length; mi++)
  {
  var replacestring = '~' + myArray1[mi];
  while (arrstring.indexOf(replacestring) > -1)
    {
    arrstring = arrstring.replace(replacestring, '');
    }
  }
arrstring = arrstring.substr(1);
var myArray3 = arrstring.split('~');

Lee

RE: How to find duplicates in two arrays

Ignore that last posting of mine.  Done too early in the morning.

Lee

RE: How to find duplicates in two arrays

(OP)
Lee,
Not sure what you meant by "Ignore that last posting of mine."

The code you wrote works except that it also eliminates the element "Thanks".

Now, as I am very new to this, I'll have to study carefully what you wrote to see why it works to make an array with one element but fix it so that "Thanks" and "guys" are the two elements in the third array.

Tom

RE: How to find duplicates in two arrays

Here's a variation on my last attempt that SHOULD work

CODE

var myArray1 = new Array("tanks","Banks", "Thanks", "men", "women", "gals")
var myArray2 = new Array("men","tanks", "guys","Banks", "women", "gals")
var arrstring1 = '~' + myArray1.join('~')
var arrstring2 = '~' + myArray2.join('~');

for (var mi = 0; mi < myArray1.length; mi++)
  {
  var replacestring = '~' + myArray1[mi];
  if (arrstring2.indexOf(replacestring) > -1)
    {
    arrstring1 = arrstring1.replace(replacestring, '');
    arrstring2 = arrstring2.replace(replacestring, '');
    }
  }
var arrstring = (arrstring1 + arrstring2).substr(1);
var myArray3 = arrstring.split('~');

What this does is create a string out of each of your original arrays, concatenated with a character that will NOT be in any of the original values.  Then moving through each element in one array, you check to see if the value is in the string made from the second array.  If it is, delete it from both strings.  After moving through the one array, concatenate the strings and remove the initial separator you used.  Then split that string into your final array of unique values.

Lee

RE: How to find duplicates in two arrays

(OP)
Lee,
Thanks. This works exactly except for one minor problem.

You have written:
arrstring1 = arrstring1.replace(replacestring, ' ');

If I take out the space in the second argument of both replace() functions then I get only one separator between the two remaining elements in the array rather than an additional one before the first element.

I hope this bit of code helps others. I now have to put this into InDesign where I am trying to find the differences between certain objects in two different documents where they in fact should be the same. It doesn't work yet (me not understanding the Adobe's object model), but that is discussion for a different forum. Thanks again.

Tom

RE: How to find duplicates in two arrays

There is no space inside the single quotes in my last example.  As well, note that I added a separator at the beginning of each of the arrstring variables initially.  Both problems you encountered were from what you typed in, not the example.

It sounds like you didn't copy and paste what I wrote.  If you're going to do that, you need to pay attention to details with what's written in the example.

Lee

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close