Here is a nested loop method for removing duplicates in array and preserving original order of elements.
var array = [1, 3, 2, 1, [5], 2, [4]]; // INPUT
var element = 0;
var decrement = array.length - 1;
while(element < array.length) {
while(element < decrement) {
if (array[element] === array[decrement]) {
array.splice(decrement, 1);
decrement--;
} else {
decrement--;
}
}
decrement = array.length - 1;
element++;
}
console.log(array);// [1, 3, 2, [5], [4]]
Explanation:
The inner loop compares first element of array with all other elements starting with element at the highest index. Decrementing towards the first element, a duplicate is spliced from the array.
When the inner loop is finished, the outer loop increments to the next element for comparison and resets the new length of the array.