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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to delete all duplicates in two arrays

Status
Not open for further replies.

ttomasko

Programmer
Aug 6, 2008
28
Hello,

I have been tearing my hair out on how to solve this. I will have an array of numbers. I'll never know the starting and ending numbers but they will be in order. However, there may be gaps in the order. It is the gaps I am interested in.

Let's say I start with the following array:

var a = [11,12,13,15,16,19,20]; //missing 14, 17, 18

I figured that the best thing would be to get an array of all whole numbers that start and end with the start and end numbers in "a." Here is how I create that array:

var aFirst = a[0];
var aLast = a[a.length-1];
//2. define length of new array
var b = 1+aLast - aFirst;
//3. following makes array of whole numbers between first and last numbers of a array
var c = [];
for(var i = 0; b > i; i++){
c.push(aFirst +i);
}//end for

//c = [11,12,13,14,15,16,17,18,19,20]

Now what stumps me is the next thing to do: how to compare those two arrays and make an array of the missing numbers.

Or maybe there is a simpler way to find the gaps in the first array without having to create a second one.

Thanks,
Tom

 
Or maybe there is a simpler way to find the gaps in the first array without having to create a second one.

Very much so. Given you know the starting and ending numbers, why not simply loop through the master array from index 1, and if the number in question is more than 1 away from what the number should have been, it gives you your missing number (or numbers, if the range is > 1).

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Dan,

Your advice helps, though I did not completely understand it. The following finds any gaps but returns only the first missing number where there are more than one missing numbers.

var a = [11,12,13,15,16,19,20];
var c = [];
for(var i =0; a.length > i; i++){
if(a + 1 < a[i+1]){
var b = a +1;
c.push(b);
}//end if
}//end for i
c; //returns 14, 17 but not 18

Tom
 
Perhaps my sentence "is more than 1 away from what the number should have been" could have been clearer.

I had envisaged keeping track of what the last number should have been in a separate variable, rather than using the loop index all the time. This would allow you to have multiple gaps in the range with no problem at all.

Alternatively, you could keep track of how many numbers are missing in the current range and use that instead of "i+1".

Shout if I'm still not being clear.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Dan,

I'm still not sure what you are getting at. You seem to say I do not have to iterate thru the array. However, in the meantime I have solved it:

var a = [11,12,13,15,16, 19,20];
var c = [];
var gapArr = [];
for(var i =0; a.length > i; i++){
if(a + 1 < a[i+1]){
var gapLength = a[i+1] - a-1;
for(var j =0; gapLength > j; j++){
var d = a+1+j;
gapArr.push(d);
}//end for j
}//end if
}//end for i
gapArr; //array of 14, 17, 18, the missing numbers

Tom
 
That's pretty much what I had come up with:

Code:
var a = [11,12,13,15,16, 19,20];
var gapArr = [];
var whatCurrentNumberShouldBe = a[0] + 1;

for(var loop=1; loop<a.length; loop++) {
	if (a[loop] > whatCurrentNumberShouldBe) {
		for (var gapLoop = whatCurrentNumberShouldBe; gapLoop != a[loop]; gapLoop++) {
			gapArr.push(gapLoop);
		}
		whatCurrentNumberShouldBe = gapLoop + 1;
	} else {
		whatCurrentNumberShouldBe++;
	}
}

console.log(gapArr);

I don't think there's much in it, though... so I'd go with whatever code you're happier understanding and maintaining.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top