How to remove duplicates element from array

In this post i’m going to remove duplicates element from array,i will try with multiple approaches to remove duplicates element from array.

Method 1
Using Set

const array = [“a”,”b”,”c”,”d”,”a”,”c”,”e”,”f”];
const m1 = […new Set(array)];
console…


This content originally appeared on DEV Community and was authored by Abu Jaid

In this post i'm going to remove duplicates element from array,i will try with multiple approaches to remove duplicates element from array.

Method 1
Using Set

const array = ["a","b","c","d","a","c","e","f"];
const m1 = [...new Set(array)];
console.log(m1);
// ["a", "b", "c", "d", "e", "f"] 

Method 2
Using object

const array = ["a","b","c","d","a","c","e","f"];
let obj = {};
for (let arr of array){
obj[arr]=true;
}
console.log(Object.keys(obj));
// ["a", "b", "c", "d", "e", "f"] 

Method 3
Using Loop

const array = ["a","b","c","d","a","c","e","f"];
const m3 = [];
for(var i = 0;i<array.length;i++){
const arr = array[i];
m3.indexOf(arr) === -1 && m3.push(arr);
}
console.log(m3)
// ["a", "b", "c", "d", "e", "f"]

Method 4
Using Filter

const array = ["a","b","c","d","a","c","e","f"];
const m4 = array.filter((el,index)=>array.indexOf(el) == index);
console.log(m4);
// ["a", "b", "c", "d", "e", "f"]


This content originally appeared on DEV Community and was authored by Abu Jaid


Print Share Comment Cite Upload Translate Updates
APA

Abu Jaid | Sciencx (2021-06-13T14:32:40+00:00) How to remove duplicates element from array. Retrieved from https://www.scien.cx/2021/06/13/how-to-remove-duplicates-element-from-array/

MLA
" » How to remove duplicates element from array." Abu Jaid | Sciencx - Sunday June 13, 2021, https://www.scien.cx/2021/06/13/how-to-remove-duplicates-element-from-array/
HARVARD
Abu Jaid | Sciencx Sunday June 13, 2021 » How to remove duplicates element from array., viewed ,<https://www.scien.cx/2021/06/13/how-to-remove-duplicates-element-from-array/>
VANCOUVER
Abu Jaid | Sciencx - » How to remove duplicates element from array. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/13/how-to-remove-duplicates-element-from-array/
CHICAGO
" » How to remove duplicates element from array." Abu Jaid | Sciencx - Accessed . https://www.scien.cx/2021/06/13/how-to-remove-duplicates-element-from-array/
IEEE
" » How to remove duplicates element from array." Abu Jaid | Sciencx [Online]. Available: https://www.scien.cx/2021/06/13/how-to-remove-duplicates-element-from-array/. [Accessed: ]
rf:citation
» How to remove duplicates element from array | Abu Jaid | Sciencx | https://www.scien.cx/2021/06/13/how-to-remove-duplicates-element-from-array/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.