-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfindDeletedNumber.php
More file actions
23 lines (19 loc) · 860 Bytes
/
findDeletedNumber.php
File metadata and controls
23 lines (19 loc) · 860 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
// An ordered sequence of numbers from 1 to N is given. One number might have deleted from it, then the remaining numbers were mixed. Find the number that was deleted.
//
// Example:
//
// The starting array sequence is [1,2,3,4,5,6,7,8,9]
// The mixed array with one deleted number is [3,2,4,6,7,8,1,9]
// Your function should return the int 5.
// If no number was deleted from the array and no difference with it, your function should return the int 0.
//
// Note that N may be 1 or less (in the latter case, the first array will be []).
function findDeletedNumber(array $arr, array $mixedArr): int {
return count($arr) === count($mixedArr) ? 0 : array_values(array_diff($arr,$mixedArr))[0];
}
// Alternative Solution:
// function findDeletedNumber(array $arr, array $mixedArr): int {
// return array_sum($arr) - array_sum($mixedArr);
// }
?>