javascript get duplicates in array
let arr =["a","b","c"];
// ES6 way
const duplicate = [...arr];
// older method
const duplicate = Array.from(arr);function getDuplicateArrayElements(arr){
var sorted_arr = arr.slice().sort();
var results = [];
for (var i = 0; i < sorted_arr.length - 1; i++) {
if (sorted_arr[i + 1] === sorted_arr[i]) {
results.push(sorted_arr[i]);
}
}
return results;
}
var colors = ["red","orange","blue","green","red","blue"];
var duplicateColors= getDuplicateArrayElements(colors);//["blue", "red"]var input = [1, 2, 3, 1, 3, 1];
var duplicates = input.reduce(function(acc, el, i, arr) {
if (arr.indexOf(el) !== i && acc.indexOf(el) < 0) acc.push(el); return acc;
}, []);
document.write(duplicates); // = 1,3 (actual array == [1, 3])[1, 2, 2, 4, 3, 4].filter((e, i, a) => a.indexOf(e) !== i) // [2, 4]
Also in JavaScript:
- implement the remove property function
- funcao com o botao esc do teclado javascript
- jquery select element with data
- Convert JS date time to SQLSERVER datetime
- learn vue js
- javascript vérifier si une chaine de carctère commence par une majuscule
- js blur element
- javascript increment alphabet
- javascript .tolowercase
- on reload js
- does json only support ascii
- how to create a new angular project in visual studio code
- how to check if a number is even or odd in javascript
- push tr in tbody using jquery
- javascript go to top of page
- discord.js send message to specific channel
- cli run js
- How do you call a functional component in React
- js extract options from select
- try catch javascript
- js select element by css selector
- js remove child elements
- new line in js
- jquery to set value in select2 dropdown button