Skip to content

Instantly share code, notes, and snippets.

@kattak
Created January 30, 2017 05:17
Show Gist options
  • Save kattak/c4303f3d7e074dd04df14a7b855a1185 to your computer and use it in GitHub Desktop.
Save kattak/c4303f3d7e074dd04df14a7b855a1185 to your computer and use it in GitHub Desktop.
Some basic JS algo problems

#JS Algorithm Problems

Write a method no_dupes which will take an array and return that same array with all duplicate items removed. Assume integers. This should be done without using uniqueness methods like Ruby's uniq.

// No Dupes in action:

// no_dupes( [ 1, 4, 2, 7, 3, 1, 2, 8 ] ); // #=> [ 1, 4, 2, 7, 3, 8 ]

no_dupes( [ 100, 32, 44, 44, 23, 32, 44 ] ) // #=> [ 100, 32, 44, 23 ]

function no_dupes(arr) { //iterate through array i = 0; while (i <= arr.length - 1){ var elem = arr[i]; var firstIndex = arr.indexOf(elem); var secondIndex = arr.indexOf(elem,firstIndex+1); if (secondIndex !== -1){ arr.splice(secondIndex,1); } i++; } return arr; }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment