Solution1:
function find_duplicate_in_array(arra1) {
var object = {};
var result = [];
arra1.forEach(function (item) {
if(!object[item])
object[item] = 0;
object[item] += 1;
})
for (var prop in object) {
if(object[prop] >= 2) {
result.push(prop);
}
}
return result;
}
console.log(find_duplicate_in_array([1, 2, 2, -2, 4, 5, 4, 7, 8, 7, 7, 71, 3, 6]));
Output: ["2","4","7"]
Solution2:
function likeRepeat(array) {
const arrayDuplicated = [];
let element;
let times = 0;
for(let i = 0 ; i < array.length ; i++) {
for(let j = 0 ; j < array.length ; j++) {
if(array[i] === array[j]) {
times++;
element = array[i];
}
if(times == 2) {
arrayDuplicated.push(element);
}
}
times = 0;
}
console.log([...new Set(arrayDuplicated)]);
}
const array = ["dos", "dos", 5, 2, true, true, false, false, 7, 8, "quirim", 10, "aquaman", "aquaman"];
likeRepeat(array);
Output: ["dos",true,false,"aquaman"]
["2","4"
["2","4","7"],"7"]
No comments:
Post a Comment
Do you think it could be useful for you? Share your thoughts with us!