Finding a Missing ASCII Character in a Array Using PHP
Top PHP Assessment and Interview Questions

Finding a Missing ASCII Character in a Array Using PHP

Program to Find a Missing ASCII Character in a Shuffled Array Using PHP

Author
Richard Mendes
March 05, 2025 • 5 mins

Finding a Missing ASCII Character in a Randomized Array Using PHP

In this post, we'll walk through a clever PHP script that randomly selects ASCII characters, removes one, and then determines which character is missing, without explicitly tracking it.

Function to Generate a Random ASCII Array

function generateRandomAsciiArray(string $startChar, string $endChar): array {
$start = ord($startChar);
$end = ord($endChar);
$asciiArray = range($start, $end);
shuffle($asciiArray);
return array_map('chr', $asciiArray);
}

This function takes two characters, $startChar and $endChar, as input and converts them into their corresponding ASCII values using the ord() function. It then generates an array of numbers ranging from the ASCII value of $startChar to $endChar using the range() function. After that, the array is shuffled randomly to mix the values. Finally, the numbers in the array are converted back to characters using array_map('chr', $asciiArray), resulting in an array of randomized characters.

Generating the ASCII Character Set

$asciiArray = generateRandomAsciiArray(',', '|');

Here, the function is called with , (ASCII 44) and | (ASCII 124), generating a shuffled array of ASCII characters between these values.

Removing a Random Character

$removedIndex = array_rand($asciiArray);
$removedChar = $asciiArray[$removedIndex];
unset($asciiArray[$removedIndex]);

The array_rand($asciiArray) function selects a random index from the array. The character at this index is then stored in the variable $removedChar. Following this, the unset() function is used to remove the character from the array, leaving a missing value behind.

Finding the Missing Character Efficiently

$sumExpected = array_sum(range(44, 124));
$sumActual = array_sum(array_map('ord', $asciiArray));
$missingChar = chr($sumExpected - $sumActual);

Instead of checking each character manually, a sum-based approach is used to find the missing character. The range(44, 124) function generates all the ASCII values in the original range, and array_sum() calculates the expected sum of these values. The array_map('ord', $asciiArray) function converts the modified array back to their ASCII values. By subtracting the actual sum (sumActual) from the expected sum (sumExpected), we can determine the ASCII value of the missing character. Finally, the chr() function converts this ASCII value back into a readable character.

Here is the full code of the example

<?php
function generateRandomAsciiArray(string $startChar, string $endChar): array {
$start = ord($startChar);
$end = ord($endChar);
$asciiArray = range($start, $end);
shuffle($asciiArray);
return array_map('chr', $asciiArray);
}

// Function to generate ascii array
$asciiArray = generateRandomAsciiArray(',', '|');
$removedIndex = array_rand($asciiArray);
$removedChar = $asciiArray[$removedIndex];
unset($asciiArray[$removedIndex]);

// Find missing character via sum comparison
$sumExpected = array_sum(range(44, 124));
$sumActual = array_sum(array_map('ord', $asciiArray));
$missingChar = chr($sumExpected - $sumActual);

echo "The missing character is: $missingChar\n";
echo "The removed character was: $removedChar\n";
?>







Latest Articles