4

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 returnarray(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.

4
  • Shows up 3 times or more consecutively or anywhere in the array? Commented Dec 10, 2014 at 7:22
  • Do $values = array_filter($values, function($value) { return $value <= 3; }) after your array_count_values(). but your function then wants to return array_keys($values) Commented Dec 10, 2014 at 8:16
  • You haven't replied to any of the answers; are we even understanding the problem correctly? Commented Dec 15, 2014 at 14:12
  • Yes, Jack I think some of them did. I got my problem already solved though. I will mark the correct answer soon. Commented Dec 18, 2014 at 1:38

6 Answers 6

1

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]);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

This should work :

function removeDuplicate($array) {
    foreach ($array as $key => $val) {
        $new[$val] ++;
        if ($new[$val] >= 3)
            unset($array[$key]);
    }
    return $array;
}

Comments

1

run this function i hope this help..

function removeDuplicate($array){
    $result = array_count_values( $array );
    $dub = array();
    $answer = array();
    foreach($result as $key => $val) {
        if($val >= 3) {
            $dub[] = $key;
        }
    }
    foreach($array as $val) {
        if(!in_array($val, $dub)) {
            $answer[] = $val;
        }
    }
    return $answer;
}

Comments

1

You can use this function with any number of occurrences you want - by default 3

function removeDuplicate($arr, $x = 3){
    $new = $rem = array();
    foreach($arr as $val) {
        $new[$val]++;
        if($new[$val]>=$x){
            $rem[$val]=$new[$val];
        }
    }
    $new = array_keys(array_diff_key($new, $rem));
    return $new;
}

Comments

1

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.

Comments

0

You should use array_unique funciton.

<?php
    $q = array(1, 3, 5, 2, 6, 6, 6, 3, 1, 9);
    print_r(array_unique($q));
?>

Try it and let me know if it worked.

1 Comment

Also, this will return [1, 3, 5, 2, 6, 9] which is not what OP wanted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.