PHP program to find missing elements from an array
Last Updated :
10 Jul, 2024
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
// 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
)
Using Array Keys
In the Array Keys approach, create an associative array using array_fill_keys() with the given array values as keys. Then, iterate through the expected range and use array_key_exists() to identify missing elements, appending them to the result array if they don't exist.
Example
PHP
<?php
function findMissingElements($array, $start, $end) {
$arrayKeys = array_fill_keys($array, true);
$missingElements = [];
for ($i = $start; $i <= $end; $i++) {
if (!array_key_exists($i, $arrayKeys)) {
$missingElements[] = $i;
}
}
return $missingElements;
}
// Example usage
$array = [1, 2, 4, 6, 7];
$start = 1;
$end = 7;
$missing = findMissingElements($array, $start, $end);
print_r($missing);
?>
OutputArray
(
[0] => 3
[1] => 5
)
Using array_flip()
Use array_flip() to flip the original array, making the values as keys. Iterate through the expected range and check if each element exists in the flipped array. If not, add it to the missing elements array.
Example
PHP
<?php
function findMissingElementsWithFlip($arr) {
$min = min($arr);
$max = max($arr);
$flippedArray = array_flip($arr);
$missingElements = [];
for ($i = $min; $i <= $max; $i++) {
if (!isset($flippedArray[$i])) {
$missingElements[] = $i;
}
}
return $missingElements;
}
// Examples
$arr1 = [1, 2, 3, 4, 6, 7, 8];
print_r(findMissingElementsWithFlip($arr1));
$arr2 = [10, 11, 14, 15];
print_r(findMissingElementsWithFlip($arr2));
?>
OutputArray
(
[0] => 5
)
Array
(
[0] => 12
[1] => 13
)
Similar Reads
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
TCP/IP Model The TCP/IP model is a framework that is used to model the communication in a network. It is mainly a collection of network protocols and organization of these protocols in different layers for modeling the network.It has four layers, Application, Transport, Network/Internet and Network Access.While
7 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Basics of Computer Networking A computer network is a collection of interconnected devices that share resources and information. These devices can include computers, servers, printers, and other hardware. Networks allow for the efficient exchange of data, enabling various applications such as email, file sharing, and internet br
14 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Unified Modeling Language (UML) Diagrams Unified Modeling Language (UML) is a general-purpose modeling language. The main aim of UML is to define a standard way to visualize the way a system has been designed. It is quite similar to blueprints used in other fields of engineering. UML is not a programming language, it is rather a visual lan
14 min read