JSFiddle - React, Tailwind, and code Playground
by ronilan
HTML
// Amazon interview
JavaScript
// Amazon interview
//There are two integer arrays
//example input:
//[66, 2, 3, 4, 5, 6]
//and
//[34, 1, 2, 3, 4, 5]
//Could you write a function to concat those two arrays to one array without duplicated elements.
//example output:
//[66, 34, 2, 1, 3, 4, 5, 6]
var arrA = [66, 2, 3, 4, 5, 6, 9];
var arrB = [34, 1, 2, 3, 4, 5];
function concat(arrA, arrB) {
var result = [];
var i = 0;
function isUnique(item) {
return result.indexOf(item) === -1 ? true : false;
}
var max = arrA.length > arrB.length ? arrA.length : arrB.length;
for (i = 0; i < max; i++) {
if (isUnique(arrA[i])) {
result.push(arrA[i]);
}
if (isUnique(arrB[i])) {
result.push(arrB[i]);
}
}
return result;
}
console.log(concat(arrA, arrB));