remove duplicates from array

Solutions on MaxInterview for remove duplicates from array by the best coders in the world

showing results for - "remove duplicates from array"
Klara
28 May 2016
1<?php
2$fruits_list = array('Orange',  'Apple', ' Banana', 'Cherry', ' Banana');
3$result = array_unique($fruits_list);
4print_r($result);
5?>
6  
7Output:
8
9Array ( [0] => Orange [1] => Apple [2] => Banana [3] => Cherry ) 
Kendrick
04 Oct 2016
1const names = ['John', 'Paul', 'George', 'Ringo', 'John'];
2
3let unique = [...new Set(names)];
4console.log(unique); // 'John', 'Paul', 'George', 'Ringo'
Alessandra
26 Mar 2016
1let arr = [1,2,3,1,1,1,4,5]
2    
3let filtered = arr.filter((item,index) => arr.indexOf(item) === index)
4    
5 console.log(filtered) // [1,2,3,4,5]
María
13 Jan 2018
1// 1. filter()
2function removeDuplicates(array) {
3  return array.filter((a, b) => array.indexOf(a) === b)
4};
5// 2. forEach()
6function removeDuplicates(array) {
7  let x = {};
8  array.forEach(function(i) {
9    if(!x[i]) {
10      x[i] = true
11    }
12  })
13  return Object.keys(x)
14};
15// 3. Set
16function removeDuplicates(array) {
17  array.splice(0, array.length, ...(new Set(array)))
18};
19// 4. map()
20function removeDuplicates(array) {
21  let a = []
22  array.map(x => 
23    if(!a.includes(x) {
24      a.push(x)
25    })
26  return a
27};
28/THIS SITE/ "https://dev.to/mshin1995/back-to-basics-removing-duplicates-from-an-array-55he#comments"
Caterina
23 Apr 2020
1const names = ['John', 'Paul', 'George', 'Ringo', 'John'];
2
3let unique = [...new Set(names)];
4console.log(unique); // 'John', 'Paul', 'George', 'Ringo'
5
Matías
02 Oct 2019
1// Use to remove duplicate elements from the array
2
3const numbers = [2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5]
4
5console.log([...new Set(numbers)])
6
7// [2, 3, 4, 5, 6, 7, 32]
similar questions
queries leading to this page
remove duplicates from array