Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to find duplicates in two arrays

Status
Not open for further replies.

TomTo

Technical User
Nov 8, 2006
6
US
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
 
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
 
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
 
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
 
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 == myArray3[i+1]){
myArray3 = "";
myArray3[i+1] = "";
}
}
window.alert(myArray3)
 
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 [b][red]- 1[/red][/b]; 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
 
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
 
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
 
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
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top