function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
// usage example:
let a = ['a', 1, 'a', 2, '1'];
let unique = a.filter(onlyUnique);
console.log(unique); // ['a', 1, 2, '1']
function withoutDuplicates (arr) {
return [...new Set(arr)];
}
let Arr = [1,2,2,2,3,4,4,5,6,6];
//way1
let unique = [...new Set(Arr)];
console.log(unique);
//way2
function removeDuplicate (arr){
return arr.filter((item,index)=> arr.indexOf(item) === index);
}
removeDuplicate(Arr);
const removeDuplicates = (arr) => [...new Set(arr)];
console.log(removeDuplicates([1, 2, 3, 3, 4, 4, 5, 5, 6]));
// Result: [ 1, 2, 3, 4, 5, 6 ]
/* amethod to use in class LinkedList
to remove duplicate node*/
removeDuplicate() {
if (this.head) {
let current = this.head;
while (current.next != null) {
if (current.value == current.next.value)
current.next = current.next.next;
else
current = current.next;
}
}
return this;
}