# 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

4/26/2021 2:10:57 AM

Pavan priya7 Answers

New Answerlet 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)

At least specify a relevant language in the tags above rather than a copy of the question. https://code.sololearn.com/W3uiji9X28C1/?ref=app

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

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)) } }

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!!!