Created
November 20, 2019 08:46
-
-
Save DonerKebab/76dd7f7ecac130ec834181b65b6e6bbb to your computer and use it in GitHub Desktop.
for little chicken
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function removeDuplicatedItem(inputArray) { | |
// create a new array to store unique items | |
var unduplicatedArray = [inputArray[0]]; | |
// start looping from 1st item cause we already put the first item into `unduplicatedArray` | |
for (let i = 1; i < inputArray.length; i++) { | |
// create a flag to store the duplicate status of item | |
var existing = false; | |
// loop through every item of unduplicatedArray | |
for (let j = 0; j < unduplicatedArray.length; j++) { | |
// if an item already exists in unduplicatedArray we then set existing = true and stop the loop | |
if (inputArray[i] == unduplicatedArray[j]) { | |
existing = true; | |
break; | |
} | |
} | |
// whenever existing = false we then put the item into unduplicatedArray | |
if (!existing) { | |
unduplicatedArray.push(inputArray[i]); | |
} | |
// remember set existing = false for next round | |
existing = false; | |
} | |
return unduplicatedArray; | |
} | |
var kq = removeDuplicatedItem([1, 1, 2, 3, 4, 5, 4]); | |
console.log(kq); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment