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
removeDuplicatedNumbersInArray [Go Back]
Purpose
The purpose of this script is to remove duplicate numbers in an array (sorted or unsorted) without performing a sort and retaining the original sequence order. 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 Remove Duplicate Numbers Question (and answer):
Given the array sequence: [6,2,2,2,4,2,5,1,1] remove the duplicated numbers without sorting.
For reference, the number sequence returned should be: [6,2,4,5,1]

Function Logic Output:


Full Working Code You Can Copy:
<html>
<head>
<script language="javascript" type="text/javascript">
function removeDuplicatedNumbersInArray(sequence) {
	var filteredSequence = [];
	/* Remove potential duplicate element values from array and retain original ordering */
	for (var o = 0; o < sequence.length; o++) {
		if (filteredSequence.length == 0) {
			filteredSequence.push(sequence[o]);
		}
		else {
			var foundDuplicate = false;
			for (var f = 0; f < filteredSequence.length; f++) {
				if (sequence[o] == filteredSequence[f]) {
					foundDuplicate = true;
				}
			}
			if (foundDuplicate == false) {
				filteredSequence.push(sequence[o]);
			}
		}
	}
	return(filteredSequence);
}
</script>
</head>
<body onload='javascript:document.getElementById("output").value = removeDuplicatedNumbersInArray([6,2,2,2,4,2,5,1,1]);'>
	Given the array sequence: [6,2,2,2,4,2,5,1,1] remove the duplicated numbers without sorting.<br />
	<span style="font-size:9pt">For reference, the number sequence returned should be: [6,2,4,5,1]</span>
	<br /><br />
	<strong>Function Logic Output:</strong><br />
	<textarea name="output" id="output" rows="4" cols="30"></textarea>
</body>
</html>
About Joe