Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Counter solution in Clear category for [old] Non-unique Elements by Joni_Braslaver
"use strict";
function nonUniqueElements(data) {
/** counts the number of time an int appears in the input **/
const counter = data.reduce((counter,int) =>{
counter[int] = counter[int] ? counter[int] + 1 : 1
return counter
},{})
/** filters the non unique numbers **/
const output = data.filter(int => counter[int] !== 1)
return output;
}
var assert = require('assert');
if (!global.is_checking) {
assert.deepEqual(nonUniqueElements([1, 2, 3, 1, 3]), [1, 3, 1, 3], "1st example");
assert.deepEqual(nonUniqueElements([1, 2, 3, 4, 5]), [], "2nd example");
assert.deepEqual(nonUniqueElements([5, 5, 5, 5, 5]), [5, 5, 5, 5, 5], "3rd example");
assert.deepEqual(nonUniqueElements([10, 9, 10, 10, 9, 8]), [10, 9, 10, 10, 9], "4th example");
console.log("Coding complete? Click 'Check' to review your tests and earn cool rewards!");
}
Oct. 4, 2019