Generation

generate functionMon, 02 Jan 2023

The input nums is supposed to be an array of unique integers ranging from 1 to mums.length (inclusive), However, there's a mistake one of the numbers in the arrays 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, 11.3is present twice and 2 is missing so 312-5 should be returned

Script function sumMissingAndDuplicate(nums) { const sum = nums.reduce((a, b) => a + b, 0); const n = nums.length; const sumWithoutDuplicate = n * (n + 1) / 2; const duplicate = sum - sumWithoutDuplicate; const missing = duplicate - sumWithoutDuplicate; return duplicate + missing; } sumMissingAndDuplicate([4, 3, 3, 11]);

Questions about programming?Chat with your personal AI assistant