Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
2 for-loop solution solution in Clear category for [old] Non-unique Elements by liketurbo
"use strict";
function nonUniqueElements(data) {
const nonUniq = []
for (let i = 0; i < data.length; i++)
for (let j = 0; j < data.length; j++) {
if (i === j) continue
if (data[i] === data[j]) {
nonUniq.push(data[i])
break
}
}
return nonUniq
}
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!");
}
"use strict";
function nonUniqueElements(data) {
const output = []
for (let i = 0; i < data.length; i++)
for (let j = 0; j < data.length; j++) {
if (i === j) continue
if (data[i] === data[j]) {
output.push(data[i])
break
}
}
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!");
}
March 16, 2020
Comments: