Main Page
 The gatekeeper of reality is
 quantified imagination.

Stay notified when site changes by adding your email address:

Your Email:

Bookmark and Share
Email Notification
findMissingNumbersInArray [Go Back]
Purpose
The purpose of this script is to find the missing numbers in an array (sorted or unsorted) given a single-dimensional base array (sorted or unsorted) that serves as a complete sequence. Over the decades of programming I've never needed to solve such a task but I was asked about it so I provide one approach (using javascript) below.

The Missing Numbers Question (and answer):
Given a base array sequence: [1,2,3,4,5,6]
find the missing numbers in the fragmented array sequence: [2,3,1,0,5]
that are present in the base array but do not exceed the highest value in the fragmented array sequence.
For reference, the missing numbers returned should be: [4]

Function Logic Output:


Full Working Code You Can Copy:
<html>
<head>
<script language="javascript" type="text/javascript">
function findMissingNumbersInArray(sequenceA, sequenceB) {
	var missingSequenceOfNumbers = [];
	/* Sort the sequences passed to this function */
	sequenceA.sort(function(a, b) {return a - b});
	sequenceB.sort(function(a, b) {return a - b});
	/* Iterate through the base array containing the complete sequence */
	for (var b = 0; b < sequenceA.length; b++) {
		var baseValue = sequenceA[b];
		if (baseValue <= sequenceB[(sequenceB.length - 1)]) {
			/* Iterate through the array containing the fragmented sequence */
			var baseValueFound = false;
			for (var f = 0; f < sequenceB.length; f++) {
				var fragmentValue = sequenceB[f];
				if (baseValue == fragmentValue) {
					baseValueFound = true;
				}
			}
			/* Save the missing number in the fragmented sequence */
			if (baseValueFound == false) {
				missingSequenceOfNumbers.push(baseValue);
			}
		}
	}
	return(missingSequenceOfNumbers);
}
</script>
</head>
<body onload='javascript:document.getElementById("output").value = findMissingNumbersInArray([1,2,3,4,5,6], [2,3,1,0,5]);'>
	Given a base array sequence: [1,2,3,4,5,6]<br />
	find the missing numbers in the fragmented array sequence: [2,3,1,0,5]<br />
	that are present in the base array but do not exceed the highest value in the fragmented array sequence.<br />
	<span style="font-size:9pt">For reference, the missing numbers returned should be: [4]</span>
	<br /><br />
	<strong>Function Logic Output:</strong><br />
	<textarea name="output" id="output" rows="4" cols="30"></textarea>
</body>
</html>
About Joe