Duplicated and missing number in an array | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 3

Duplicated and missing number in an array

The input nums is supposed to be an array of unique integers ranging from 1 to nums.length (inclusive). However, there is a mistake: one of the numbers in the array duplicated, which means another number is missing. Find and return the sum of the duplicated number and the missing number. Example in the array [4, 3, 3, 1], 3is present twice and 2 is missing, so 3 +2= 5 should be returned. // please write return statement//no matter which language its up to you

26th Apr 2021, 2:10 AM
Pavan priya
Pavan priya - avatar
7 Answers
+ 6
let duplicateValue = 0 let missingValue = 0 let sortData = arr.sort((a,b)=>b>a?-1:1) sortData.forEach((ele,index)=>{ if(arr.indexOf(ele) !== index){ duplicateValue = ele } if(ele !== index+1){ missingValue = index + 1 } }) console.log(duplicateValue+missingValue)
16th Sep 2021, 5:23 AM
Gautam
0
At least specify a relevant language in the tags above rather than a copy of the question. https://code.sololearn.com/W3uiji9X28C1/?ref=app
26th Apr 2021, 4:35 AM
Ipang
0
Here's an algorithmic approach example that should be easy to rewrite in any language, with an explanation for how to achieve O(n) Time and O(1) space complexities. https://code.sololearn.com/cYmujdEYEsS9/?ref=app
26th Apr 2021, 6:34 AM
ChaoticDawg
ChaoticDawg - avatar
0
In Which language you wrote this code
24th Aug 2021, 1:19 PM
hima bindu
hima bindu - avatar
0
let arr=[1,2,3,5,5,6] for(let i=0;i<arr.length;i++){ if(arr[i+1]==arr[i]){ console.log(arr[i]+(i+1)) } }
16th Sep 2021, 5:22 AM
Gautam
0
Hii
4th Dec 2021, 6:30 AM
Pompi Nath
- 1
l=[1,3,3,4] c=set([x for x in l if l.count(x)>1]) rep_nums=[] for i in c: rep_nums.append(i) missing_num = 0 for i in range(len(l)): if i+1 == l[i]: pass else: missing_num = i+1 print(missing_num+sum(rep_nums)) HOPE IT WORKS!!!
26th Apr 2021, 4:12 AM
sarada lakshmi
sarada lakshmi - avatar