-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveDuplicateArray.js
More file actions
28 lines (25 loc) · 1.05 KB
/
RemoveDuplicateArray.js
File metadata and controls
28 lines (25 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
* Function to remove duplicates elements from an array
* @param {Array} arr that be contains string, number or bolean (can be work with objects)
* @return {Array} return array without duplicates elements
*/
function removeDuplicate(arr){
return arr.filter(function(item,index){ return arr.indexOf(item) === index});
}
/**
* ES6
* Function to remove duplicates elements from an array
* @param {Array} arr that be contains string, number or bolean (can be work with objects)
* @return {Array} return array without duplicates elements
*/
const removeDuplicate2 = (arr) =>
arr.filter((item, index) => arr.indexOf(item) === index);
/**
* ES6 with Set method
* A Set is a collection of unique values. To remove duplicates from an array
* @param {Array} arr that be contains string, number or bolean (can be work with objects)
* @return {Array} return array without duplicates elements
*/
const removeDupplicate3 = (arr) => [...new Set(arr)];
const tab = ["test", "test", "success", "test"];
console.log(removeDuplicate(tab)); // return ["test","success"]