remove all dups and negative numbers from 3 arrays

40 views Asked by At

I recently had a technical job interview. the code challenge i was asked to solve was given 3 arrays of numbers. goal was to remove all dups and negative nums i was able to remove dups but not duplicates, so far i have the following code. what am i missing?

let array1=[10, 200, 10, 200, 100, 100, 200, 200, 200, 200, -99, -6, 0, -859]
let array2 = [100, 200, 100, 200, 689, 689, 200, 400, 210, 200, -58, 200, -305, -6, 0, -859]
let array3 =[100, 200, 100, 200, 689, 689, 200, 400, 210, 400, -6, 200, -305, -6, 0, -859]

const arrays = {
    array1,array2,array3
}


let nodups=Array.from(new Set(array1.concat(array2,array3)))
console.log(nodups);
2

There are 2 answers

1
Nina Scholz On

After getting a duplicate free array, you need to filter the array for only positive values.

var array1 = [10, 200, 10, 200, 100, 100, 200, 200, 200, 200, -99, -6, 0, -859],
    array2 = [100, 200, 100, 200, 689, 689, 200, 400, 210, 200, -58, 200, -305, -6, 0, -859],
    array3 = [100, 200, 100, 200, 689, 689, 200, 400, 210, 400, -6, 200, -305, -6, 0, -859],
    result = Array
        .from(new Set(array1.concat(array2, array3)))
        .filter(v =>  v >= 0);

console.log(result);

0
baao On

Put them in a Set and apply a filter

let array1=[10, 200, 10, 200, 100, 100, 200, 200, 200, 200, -99, -6, 0, -859]
let array2 = [100, 200, 100, 200, 689, 689, 200, 400, 210, 200, -58, 200, -305, -6, 0, -859]
let array3 =[100, 200, 100, 200, 689, 689, 200, 400, 210, 400, -6, 200, -305, -6, 0, -859]

let res = [...new Set([...array1, ...array2, ...array3])].filter(e => e >= 0);
console.log(res);