How to check if two arrays have the same set of digits in PHP

1 Answer

0 votes
function have_same_set_of_digits($arr1, $arr2) {
    $len1 = count($arr1);
    $len2 = count($arr2);
          
    if ($len1 != $len2)
        return false;
        
    for ($i = 0; $i < $len1; $i++) {
        $found = false;
        for ($j = 0; $j < $len1; $j++) {
            if ($arr1[$i] == $arr2[$j]) {
                $found = true;
                break;
            }
        }
        if (!$found)
            return false;
    }
     
    return true;
}

$arr1 = array(1, 3, 8, 5, 9, 2);
$arr2 = array(2, 9, 1, 8, 2, 5);
           
if (have_same_set_of_digits($arr1, $arr2))
    echo "Yes";
else
    echo "No";
    
    

/*
run:

No

*/

 



answered Dec 4, 2020 by avibootz
...