Pergunta de entrevista da empresa Cimpress

Interview process starts with writing a function, in this function you will have to find elements in array which are duplicates and have to return the number of count of duplicate elements. Design for pizza store with robots

Resposta da entrevista

Sigiloso

13 de abr. de 2021

For finding duplicates count in array elements, I have coded to answer your question. Please find below program in PHP Language. $array = array("one", "three", "two", "one", "one", "two", "three"); function parseArray($arr, $val2) { $count = 1; $finalArray = array(); foreach ($arr as $ar) { if ($val2 == $ar) { ++$count; } } return $count; } function checkinArray($array, $val) { foreach ($array as $v) { if ($v == $val) return true; } return false; } $checkArray = array(); $finalArray = array(); foreach ($array as $k => $val) { $newArray = $array; unset($newArray[$k]); if (!checkinArray($checkArray, $val)) { $checkArray[] = $val; $finalArray[$val] = parseArray($newArray, $val); } } //print the values foreach ($finalArray as $key => $val) echo "element $key occurs $val number of times" . PHP_EOL;