Skip to main content
avoiding duplicates in resulting array
Source Link

Javascript (ES6), 3951 bytes

a=a=>aa=>[.sort().filter((v,i)=>v===a[i+1])

Expanded:

function getDuplicates(arr) {
    // This makes use of the fact that.new sortSet() mutates the original array
    arra.sort().filter((valuev, indexi) => value === arr[index + 1]=>v===a[i+1]);
})]

run on tio.

Try it:

a=a=>aa=a=>[...new Set(a.sort().filter((v,i)=>v===a[i+1]))]


console.log(a([1,2,3]));
console.log(a([1,2,3,2,1]));
console.log(a([1.0, 2, 2.0, 1.0]));
console.log(a([1, '1', '1.0']));
console.log(a([null, null, false, false, true, true]));
console.log(a([1,1,1,3,2,3,2,3,4]));

Javascript (ES6), 39 bytes

a=a=>a.sort().filter((v,i)=>v===a[i+1])

Expanded:

function getDuplicates(arr) {
    // This makes use of the fact that sort() mutates the original array
    arr.sort().filter((value, index) => value === arr[index + 1]);
}

Try it:

a=a=>a.sort().filter((v,i)=>v===a[i+1])


console.log(a([1,2,3]));
console.log(a([1,2,3,2,1]));
console.log(a([1.0, 2, 2.0, 1.0]));
console.log(a([1, '1', '1.0']));
console.log(a([null, null, false, false, true, true]));

Javascript (ES6), 51 bytes

a=>[...new Set(a.sort().filter((v,i)=>v===a[i+1]))]

run on tio.

Try it:

a=a=>[...new Set(a.sort().filter((v,i)=>v===a[i+1]))]


console.log(a([1,2,3]));
console.log(a([1,2,3,2,1]));
console.log(a([1.0, 2, 2.0, 1.0]));
console.log(a([1, '1', '1.0']));
console.log(a([null, null, false, false, true, true]));
console.log(a([1,1,1,3,2,3,2,3,4]));

corrected
Source Link

Javascript (ES6), 4139 bytes

a=(a)=>aa=a=>a.sort().filter((v,i)=>v===a[i+1])

Expanded:

function getDuplicates(arr) {
    // This makes use of the fact that sort() mutates the original array
    arr.sort().filter((value, index) => value === arr[index + 1]);
}

Try it:

a=(a)=>aa=a=>a.sort().filter((v,i)=>v===a[i+1])


console.log(a([1,2,3]));
console.log(a([1,2,3,2,1]));
console.log(a([1.0, 2, 2.0, 1.0]));
console.log(a([1, '1', '1.0']));
console.log(a([null, null, false, false, true, true]));

Javascript (ES6), 41 bytes

a=(a)=>a.sort().filter((v,i)=>v===a[i+1])

Expanded:

function getDuplicates(arr) {
    // This makes use of the fact that sort() mutates the original array
    arr.sort().filter((value, index) => value === arr[index + 1]);
}

Try it:

a=(a)=>a.sort().filter((v,i)=>v===a[i+1])


console.log(a([1,2,3]));
console.log(a([1,2,3,2,1]));
console.log(a([1.0, 2, 2.0, 1.0]));
console.log(a([1, '1', '1.0']));
console.log(a([null, null, false, false, true, true]));

Javascript (ES6), 39 bytes

a=a=>a.sort().filter((v,i)=>v===a[i+1])

Expanded:

function getDuplicates(arr) {
    // This makes use of the fact that sort() mutates the original array
    arr.sort().filter((value, index) => value === arr[index + 1]);
}

Try it:

a=a=>a.sort().filter((v,i)=>v===a[i+1])


console.log(a([1,2,3]));
console.log(a([1,2,3,2,1]));
console.log(a([1.0, 2, 2.0, 1.0]));
console.log(a([1, '1', '1.0']));
console.log(a([null, null, false, false, true, true]));

Converted snippet into function
Source Link

Javascript (ES6), 3441 bytes

a=(a)=>a.sort().filter((v,i)=>v===a[i+1])

Expanded:

function getDuplicates(arr) {
    // This makes use of the fact that sort() mutates the original array
    arr.sort().filter((value, index) => value === arr[index + 1]);
}

Try it:

function getDupesa=(a) {
    return a=>a.sort().filter((v,i)=>v===a[i+1])
}

console.log(getDupesa([1,2,3]));
console.log(getDupesa([1,2,3,2,1]));
console.log(getDupesa([1.0, 2, 2.0, 1.0]));
console.log(getDupesa([1, '1', '1.0']));
console.log(getDupesa([null, null, false, false, true, true]));

Javascript (ES6), 34 bytes

a.sort().filter((v,i)=>v===a[i+1])

Try it:

function getDupes(a) {
    return a.sort().filter((v,i)=>v===a[i+1])
}

console.log(getDupes([1,2,3]));
console.log(getDupes([1,2,3,2,1]));
console.log(getDupes([1.0, 2, 2.0, 1.0]));
console.log(getDupes([1, '1', '1.0']));
console.log(getDupes([null, null, false, false, true, true]));

Javascript (ES6), 41 bytes

a=(a)=>a.sort().filter((v,i)=>v===a[i+1])

Expanded:

function getDuplicates(arr) {
    // This makes use of the fact that sort() mutates the original array
    arr.sort().filter((value, index) => value === arr[index + 1]);
}

Try it:

a=(a)=>a.sort().filter((v,i)=>v===a[i+1])


console.log(a([1,2,3]));
console.log(a([1,2,3,2,1]));
console.log(a([1.0, 2, 2.0, 1.0]));
console.log(a([1, '1', '1.0']));
console.log(a([null, null, false, false, true, true]));

Source Link
Loading