Skip to content

Instantly share code, notes, and snippets.

View dorival's full-sized avatar
🙃

Dorival Neto dorival

🙃
View GitHub Profile
@dorival
dorival / RemoveDuplicates
Last active August 29, 2015 13:58
Remove unsorted array duplicates using vanilla JavaScript
// Sample array
var collection = [1, 3, 7, 9, 0, 2, 5, 3, 8, 10, 2, 11, 7, 1, 7, 4, 6, 8, 1, 0];
/* O(n²) complexity on worst case scenarios, but O(n) on best scenarios */
/* 2. Resize the array with two nested loops, but it goes one forward comparing and another backwards removing
The result is a faster interaction because we are removing duplicated ahead before Forward hits it */
function BackForwardUnique(arr){
for(var forward = 0; forward < arr.length; forward++){
for (var backward = arr.length - 1; backward > forward; backward--){