Question: I was just going through these questions for PHP and got stuck at one of them. The question is:

> 
> You have a PHP 1 dimensional array. Please write a PHP function that
> takes 1 array as its parameter and returns an array. The function must
> delete values in the input array that shows up 3 times or more?
> 
> 
> For example, if you give the function
> 
> 
> array(1, 3, 5, 2, 6, 6, 6, 3, 1, 9)`the function will return`array(1, 3, 5, 2, 3, 1, 9)
> 
> 
> 

I was able to check if they are repeating themselves but I apply it to the array I am getting as input.

```
function removeDuplicate($array){
    $result = array_count_values( $array );
    $values = implode(" ",  array_values($result));
    echo $values . "<br>";  
}

$qArray = array(1, 3, 5, 2, 6, 6, 6, 3, 1, 9);
removeDuplicate($qArray);

```

One more thing, we cannot use `array_unique` because it includes the value which is repeated and in question we totally remove them from the current array.

Answer: 
Assuming the value may not appear 3+ times anywhere in the array:

```
$array = array(1, 3, 5, 2, 6, 6, 6, 3, 1, 9);

// create array indexed by the numbers to remove    
$remove = array_filter(array_count_values($array), function($value) {
    return $value >= 3;
});

// filter the original array
$results = array_values(array_filter($array, function($value) use ($remove) {
    return !array_key_exists($value, $remove);
}));

```

If values may not appear 3+ times consecutively:

```
$results = [];
for ($i = 0, $n = count($array); $i != $n;) {
    $p = $i++;
    // find repeated characters
    while ($i != $n && $array[$p] === $array[$i]) {
        ++$i;
    }

    if ($i - $p < 3) {
        // add to results
        $results += array_fill(count($results), $i - $p, $array[$p]);
    }
}

```
I think it is getting correct output. just try once.

```
 $array=array(1,2,3,7,4,4,3,5,5,6,7);
 $count=count($array);
 $k=array();
 for($i=0;$i<=$count;$i++)
 {
 if(!in_array($array[$i],$k))
 {
 $k[]=$array[$i];
 }
 }
 array_pop($k);
 print_r($k);

```

in this first $k is an empty array,after that we are inserting values into $k.