-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcheck_three_and_two.php
More file actions
30 lines (27 loc) · 929 Bytes
/
check_three_and_two.php
File metadata and controls
30 lines (27 loc) · 929 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
// 7 kyu - Check three and two
// Given an array with exactly 5 strings "a", "b" or "c" (chars in Java, characters in Fortran), check if the array contains three and two of the same values.
//
// Examples
// ["a", "a", "a", "b", "b"] ==> true // 3x "a" and 2x "b"
// ["a", "b", "c", "b", "c"] ==> false // 1x "a", 2x "b" and 2x "c"
// ["a", "a", "a", "a", "a"] ==> false // 5x "a"
function checkThreeAndTwo($arr) {
$unique = array_unique($arr);
foreach($unique as $l) {
$count = count(array_filter($arr, function($x) use($l){
return $x ===$l;
}));
if ($count !== 2 && $count !== 3) return false;
}
return true;
}
// Alternative Solutions:
// function checkThreeAndTwo($arr) {
// $counts = array_count_values($arr);
// return count($counts) === 2 && max($counts) === 3;
// }
// function checkThreeAndTwo($arr) {
// return empty(array_diff(array(2, 3), array_count_values($arr)));
// }
?>