- 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
7 Respuestas
+ 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)
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
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
0
In Which language you wrote this code
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))
}
}
0
Hii
- 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!!!