Remove Duplicates
This is a JavaScript function for removing duplicates in an array.
by Shawn Wood
HTML
<script src="//code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<link rel="stylesheet" href="//stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<script src="//stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://s3.amazonaws.com/kiddsoftware-jsfiddle/mocha-1.9.0.css">
<link rel="stylesheet" href="https://s3.amazonaws.com/kiddsoftware-jsfiddle/mocha-1.9.0.css">
<script src="https://s3.amazonaws.com/kiddsoftware-jsfiddle/mocha-1.9.0.js"></script>
<script src="https://s3.amazonaws.com/kiddsoftware-jsfiddle/chai-1.5.0.js"></script>
<script src="https://s3.amazonaws.com/kiddsoftware-jsfiddle/chai-jquery.js"></script>
<!--
Bootstrap docs: https://getbootstrap.com/docs
-->
<div class="container-fluid">
<h2>
Remove Duplicates from an Array
</h2>
<form class="form" id="arrayForm">
<div class="row" id="stringArray">
<label class="control-label col-sm-2" for="email">Array:</label>
<input type="text" name="input0" class="form-control col-sm-1" value="0">
<input type="text" name="input1" class="form-control col-sm-1" value="1">
<input type="text" name="input2" class="form-control col-sm-1" value="3">
<input type="text" name="input3" class="form-control col-sm-1" value="4">
<input type="text" name="input4" class="form-control col-sm-1" value="6">
<input type="text" name="input5" class="form-control col-sm-1" value="1">
<input type="text" name="input6" class="form-control col-sm-1" value="6">
<input type="text" name="input7" class="form-control col-sm-1" value="0">
<input type="text" name="input8" class="form-control col-sm-1" value="9">
<input type="text" name="input9" class="form-control col-sm-1" value="2">
...
CSS
.row {
background: #f8f9fa;
margin-top: 20px;
}
.col {
border: solid 1px #6c757d;
padding: 10px;
}
JavaScript
var array = [1, 2, 2, 3, 3, 5, 5, 1, 1];
let uniqueArray = Array.from(new Set(array));
// Global storage for the array
var duplicatesArray = [];
// This is a form serializer function
var formSerializer = function(formID) {
if (typeof formID === undefined && formID === null) {
formID = '#arrayForm';
}
var serilaizedForm = $(formID).serialize();
var serializedArray = serilaizedForm.split('&');
var strippedArray = [];
console.log(serilaizedForm);
console.log(serializedArray);
for (let i = 0; i < serializedArray.length; i++) {
var arrayItem = serializedArray[i].split('=');
strippedArray.push(arrayItem[1]);
}
duplicatesArray = strippedArray;
return strippedArray;
};
//This function removes duplicated from an array
var removeDuplicates = function(sourceArray){
if (typeof sourceArray === undefined && sourceArray === null) {
sourceArray = formSerializer('#arrayForm');
}
const existingArray = sourceArray;
var newArray = [];
// Loop through array values
for(i=0; i < existingArray.length; i++){
if(newArray.indexOf(existingArray[i]) === -1) {
newArray.push(existingArray[i]);
}
}
return newArray;
};
var showStringArray = function(){
var arrayResults = removeDuplicates(formSerializer('#arrayForm'));
var stringArray = '';
var arrayLength = arrayResults.length - 1;
console.log(arrayLength);
console.log(arrayResults.length);
for (let j =0; j < arrayResults.length; j++){
if(j < arrayLength){
stringArray += arrayResults[j] + ',';
} else {
stringArray += arrayResults[j];
}
}
$('#arrayResult').val(stringArray);
};
$(document).ready(function() {
//formSerializer('#arrayForm')
showStringArray();
$('#stringArray input').on('change input', function(event){
showStringArray();
});
});
/* Run Tests */
// Configure Mocha, telling both it and chai to use BDD-style tests.
mocha.setup("bdd");
chai.should();
describe('Mocha Tests', function(){
// Test reverse string function
...