Results: 1578
Notes
  • Newest first
  • Oldest first
  • Newest first(All)
  • Oldest first(All)
// Given an integer array nums, return the third distinct maximum number in this array. If the third maximum does not exist, return the maximum number.
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]);
by Luka Khitaridze
2 years ago
0
PHP
Array
0
Valid Parentheses CODE
// Determine whether parentheses are valid if they contain some kind of mathematical operations or just text
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))
by Luka Khitaridze
2 years ago
0
PHP
Syntax
0
// Given an array nums containing n distinct numbers in the range [0, n], // return the only number in the range that is missing from the array.
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]); // 2
by Luka Khitaridze
2 years ago
0
PHP
Array
0
// Given two integer arrays nums1 and nums2, return an array of their intersection. // Each element in the result must be unique and you may return the result in any order.
function 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.
by Luka Khitaridze
2 years ago
0
PHP
Array
0
isset
Check whether a variable is empty. Also check whether the variable is set/declared:
<?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.";
}
?>
by Guram Azarashvili
2 years ago
0
PHP
functions
0
// The array_combine() function creates an array by using the elements from one "keys" array and one "values" array.
$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 !!!
by Luka Khitaridze
2 years ago
0
PHP
Array
0
Length of last word CODE
<?php
function lengthOfLastWord1($s) {
        $words = str_word_count($s,1);  //returns words in array
        //(function returns an array with the words from the string) 
        $wordCount = str_word_count($s); //counts words in text 
        $lastW =  $words[$wordCount-1];//returns last word
        $lastWLen = strlen($lastW); //The last word length
        $lastResult = "The last word is ".'"'.$lastW.'"'." with length $lastWLen";
        return  $lastWLen;
        return $lastResult;      
by Guram Azarashvili
2 years ago
0
PHP
Solutions
0
Remove element without loop CODE
// The array_diff() function compares the values of two (or more) arrays, and returns the differences. // Input: nums = [0,1,2,2,3,0,4,2], val = 2 // output: nums = [0,1,3,0,4]
function removeElement($nums, $val) {
    return array_diff($nums, [$val]);   
}
print_r(removeElement([0,1,2,2,3,0,4,2], 2);
// runtime 7ms
by Luka Khitaridze
2 years ago
0
PHP
Array
0
sqrt() and floor()
function mysqrt($x){ $x=sqrt($x); $x=floor($x); return($x); } echo mySqrt($x);
by Anna Tukvadze
2 years ago
0
PHP
Object Oriented PHP Tutorial
0
array_unique($arrary) function;
1. <?php function removeDuplicates($nums){ $nums=array_unique($nums); return $nums; } print_r (removeDuplicates([3, 5, 1,2,3, 5])); ?> 2. <?php function removeDuplicates($nums){ $result=[]; foreach ($nums as $num){ if (!in_array($num, $result)){ $result[]=$num; } } return $result; } print_r (removeDuplicates([3, 5, 1,2,3, 5])); ?>
by Anna Tukvadze
2 years ago
0
PHP
26. Remove Duplicates from Sorted Array
Object Oriented PHP Tutorial
0
Results: 1578