PHP program to find missing element(s) from an array
We have to find element(s) in array that are missing from an array in range from array minimum to array maximum.
Examples:
Input : arr[] = (1, 2, 3, 4, 6, 7, 8) Output : 5 The array minimum is 1 and maximum is 8. The missing element in range from 1 to 8 is 5. Input : arr[] = (10, 11, 14, 15) Output : 12, 13
This problem can be solved by iterating in an array by observing the contiguous difference between elements. But in PHP we can make use of some inbuilt functions to solve the problem.
We will have to use the below two functions for this purpose:
- range() function: This function is used to create an array of elements of any kind such as integer, alphabets within a given range(from low to high) i.e, list’s first element is considered as low and last one is considered as high.
- array_diff() function: if we have an array of elements, we can find missing element(s) by comparing two arrays.
The idea to solve this problem using two inbuilt functions is to, first use range() function to create a new array from starting element and maximum element of original array using max() function. After this we apply array_diff() function to compare newly created array and original array thus getting all the missing elements of the original array.
<?php // PHP code to find missing elements function not_present($list) { // Create an array with range from array // minimum to maximum. $new_array = range(min($list), max($list)); // Find those elements that are present // in new_array but not in given list return array_diff($new_array, $list); } // Driver code print_r(not_present(array(1, 2, 3, 4, 7, 8))); print_r(not_present(array(10, 11, 12, 14, 15, 16))); ?> |
Output:
Array
(
[4] => 5
[5] => 6
)
Array
(
[3] => 13
)
Recommended Posts:
- Program to remove empty array elements in PHP
- How to find the index of all occurrence of elements in an array using JavaScript ?
- PHP program to find the Standard Deviation of an array
- PHP program to find the maximum and the minimum in array
- How to get the elements of one array which are not present in another array using JavaScript?
- Reset keys of array elements using PHP ?
- PHP | Remove duplicate elements from Array
- JavaScript | Add new elements at the beginning of an array
- Remove elements from a JavaScript Array
- PHP | Separate odd and even elements from array without using loop
- Sum and Product of Array elements using JavaScript
- Find Web Elements using Selenium WebDriver
- How to convert list of elements in an array using jQuery ?
- How to remove multiple elements from array in JavaScript ?
- JavaScript | Remove empty elements from an array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


