I'm working on a function which removes the duplicates in an array. The catch on this function is if the consecutive duplicates are less than 5 it will remain and 5 above will become a single number.
My problem is cant figure out the logic.
hope you can help me.
thanks.
Sample code
function removeDuplicates($array){
$result = array();
$lastVal = null;
$temp_array = array();
foreach ($array as $key => $value) {
# code...
if($value != $firstVal){
$result[] = $value;
}else{
$temp_array[] = $value;
}
$lastVal = $value;
}
return $result;
}
sample
$array = array(1,2,4,1,1,1,1,0,8,7,2,0,0,8,8,8,8,8,8,8,2,4,1,5);
expected result
[1,2,4,1,1,1,1,0,8,7,2,0,0,8,2,4,1,5]