function containsDuplicate($nums) {
$nums = array_count_values($nums);
foreach ($nums as $key => $num) {
if ($num >= 2) {
return true;
}
}
return false;
}
echo containsDuplicate([1, 2, 3, 1]);
function thirdMax($nums) {
$nums = array_unique($nums); // built-in function that removes duplicated values in array
sort($nums);
$count = count($nums);
if ($count >= 3) {
array_pop($nums);
array_pop($nums);
}
return end($nums); // built-in function that returns the last element of array
}
echo thirdMax([2, 2, 3, 1]);
function isValid($string) {
$len = strlen($string);
$filtered = '';
for ($x = 0; $x < $len; $x++) {
$ASCII = ord($string[$x]);
if (($ASCII==91 || $ASCII==93 || $ASCII==123 || $ASCII==125 || $ASCII==40 || $ASCII==41)) {
$filtered .= $string[$x];
}
}
$len2 = strlen($filtered);
if($len2%2==1){
return false;
}
$parentheses = [
"("=>")",
"["=>"]" ,
"{"=>"}",
];
$open = [];
for ($x = 0; $x < $len2; $x++) {
if (array_key_exists($filtered[$x], $parentheses)) {
array_push($open, $filtered[$x]);
} else {
$parenthesis = array_pop($open);
if ($filtered[$x] != $parentheses[$parenthesis]) {
return false;
}
}
}
if ($open) {
return false;
}
return true;
}
echo isValid("{ :'> baeknwaw adanww a} [ daw ] awda( :.a,a)a"); // returns true
echo isValid("(1 + 1) = 2"); // returns true
echo isValid("(1 + 1)) = 2"); // returns false
// enjoy with coding))
function missingNumber($nums) {
sort($nums);
foreach ($nums as $key => $num) {
if ($key != $num) {
return $key;
}
}
return count($nums);
}
echo missingNumber([0, 3, 1]); // 2
echo missingNumber([0, 1]); // 2function intersection($nums1, $nums2) {
// firstly, remove duplicates,
// you can use here array_unique function twice !
$result1 = [];
$result2 = [];
foreach ($nums1 as $num1) {
if (!in_array($num1, $result1)) {
$result1[] = $num1;
}
}
foreach ($nums2 as $num2) {
if (!in_array($num2, $result2)) {
$result2[] = $num2;
}
}
// merge two arrays
// you can use here array_merge function !
for ($i = 0; $i < count($result2); $i++) {
$result1[$i + count($result1)] = $result2[$i];
}
$array = [];
// unfortunately, I could not count the values and
// get the keys from array without built-in function
$result1 = array_count_values($result1);
foreach ($result1 as $key => $value) {
if ($value >= 2) {
$array[] = $key;
}
}
return $array;
}
print_r(intersection([4, 9, 5], [9, 4, 9, 8, 4])); // [4, 9];
// Instead of so much effort, you can just use the array_intersect() function./**
* @param Integer[] $nums
* @param Integer $target
* @return Integer[]
*/
function twoSum($nums, $target) {
// calculate length of array - $nums
$nums_len = count($nums);
// iterate over $nums array
for($i = 0; $i < $nums_len; $i++){
// find difference between $target and current number
$diff = $target - $nums[$i];
// remove that number and search index of current
// number in the $nums array
unset($nums[$i]);
$j = array_search($diff,$nums);
// if there is an element return answer
if($j != NULL){
return [$i, $j];
}
}
}
/**
* @param String $s
* @param String $t
* @return String
*/
function findTheDifference($s, $t) {
$len_s = strlen($s);
$sum_s = 0;
$sum_t = 0;
$i = 0;
// calculate sum of strings by ascii codes numbers
for(; $i < $len_s; $i++){
$sum_s += ord($s[$i]);
$sum_t += ord($t[$i]);
}
// so $len_s + 1 == $t
// length thats why
$sum_t += ord($t[$i]);
return chr($sum_t - $sum_s);
}
/**
* @param Integer[] $nums
* @return NULL
*/
function moveZeroes(&$nums) {
$nums_len = count($nums);
for($i = 0; $i < $nums_len; $i++){
if($nums[$i] === 0){
unset($nums[$i]);
$nums [] = 0;
}
}
}
<?php
$a = 0;
// True because $a is set
if (isset($a)) {
echo "Variable 'a' is set.<br>";
}
$b = null;
// False because $b is NULL
if (isset($b)) {
echo "Variable 'b' is set.";
}
?>
$firstName=["gela", "bela", "lela"];
$age = [18, 19, 20];
$keyValue = array_combine($firstName, $age);
print_r($keyValue);
// ( "gela" => 18, "bela" => 19, "lela" => 20 )
// Please note that both arrays should have equal quantity of elements !!!