DEV Community

Discussion on: Solution: Missing Number

Collapse
 
nithish_13 profile image
Nithish Kumar

Hey sean , Thank you for sharing this problem. I really like it. After seeing the problem des. , i came up with this solution. Is this a better approach?

const findMissing = arr => {
const orginal = Array.from({length:arr.length+1} , (_,i) => i);
return orginal.filter(v => arr.indexOf(v) === -1)[0]
}

Thanks in advance :)

Collapse
 
seanpgallivan profile image
seanpgallivan • Edited

The code is very readable, which is good, but indexOf() is an O(n) time function by itself, so the overall time complexity becomes O(n^2), and the space complexity becomes O(n) since you're creating a new array (original).

Collapse
 
nithish_13 profile image
Nithish Kumar

Thank you for explaining... Even though I'm not good at Big O notation, I can understand ur explanation.