duplikat nomor dalam array javascript
[1, 1, 2, 2, 3].filter((element, index, array) => array.indexOf(element) !== index) // [1, 2]
Smarty Hearty
[1, 1, 2, 2, 3].filter((element, index, array) => array.indexOf(element) !== index) // [1, 2]
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])
let arr =["a","b","c"];
// ES6 way
const duplicate = [...arr];
// older method
const duplicate = Array.from(arr);
function findUniq(arr) {
return arr.find(n => arr.indexOf(n) === arr.lastIndexOf(n));
}
console.log(findUniq([ 0, 1, 0 ]))
console.log(findUniq([ 1, 1, 1, 2, 1, 1 ]))
console.log(findUniq([ 3, 10, 3, 3, 3 ]))
console.log(findUniq([ 7, 7, 7, 20, 7, 7, 7 ]))
var counts = {};
your_array.forEach(function(x) { counts[x] = (counts[x] || 0)+1; });
let arr = [1, 7, 8, 9, 10, 20, 33, 0, 20, 7, 1]
console.log([... new Set(arr)]
// Output : [1, 7, 8, 9, 10, 20, 33, 0]