my code
let arr = [1,2,5,4,2,5, 4];
const fun = ()=>{
let newArr= [];
let el = [];
let result = 0
let i = 0;
while(arr.length > 0 ){
el = arr.splice(i,1);
el = el[0];
console.log(el)
if (!newArr.includes(el)){
newArr.push(el);
}
else if(newArr.includes(el)){
while( newArr.includes( el)){
++ el;
}
newArr.push(el);
}
}
result = newArr.reduce((el, acc)=>{
return el+acc
});
return result ;
}
fun(arr)
Top comments (0)