Q:

Write a PHP program to check if a triple is presents in an array of integers or not. If a value appears three times in a row in an array it is called a triple

0

Write a PHP program to check if a triple is presents in an array of integers or not. If a value appears three times in a row in an array it is called a triple

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

<?php
function test($nums)
{
     $arra_len = sizeof($nums) - 1;
     $n = 0;
			for ($i = 0; $i < $arra_len; $i++)
            {
                 $n = $nums[$i];
				if ($n == $nums[$i + 1] && $n == $nums[$i + 2]) return true;
            }
            return false;
 }

var_dump(test(array(1, 1, 2, 2, 1)));
var_dump(test(array(1, 1, 2, 1, 2, 3)));
var_dump(test(array(1, 1, 1, 2, 2, 2, 1)));

Sample Output:

bool(false)
bool(false)
bool(true)

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now