1

I have an array:

$arr1 = [1,2,2,3,3,3];

Is there any method by which I can delete a duplicate value once like,

some_function($arr1,3);

which gives me the ouput $arr1 = [1,2,2,3,3]

3
  • 1
    This is such a specific requirement that there isn't a built-in method. Count the number of instances in the array, if there are > 1 then find and remove one. Commented Oct 20, 2017 at 7:32
  • @JJJ Yep you are right. But could you provide me with some more details? Commented Oct 20, 2017 at 7:34
  • check answers below/ Commented Oct 20, 2017 at 8:02

3 Answers 3

2

As per the comment, check if there are more than one of the element you're searching for, then find and remove one of them.

function remove_one_duplicate( $arr, $target ) {
    if( count( array_keys( $arr, $target ) ) > 1 ) {
        unset( $arr[array_search( $target, $arr )] );
    }

    return $arr;
}
Sign up to request clarification or add additional context in comments.

1 Comment

As for this question, here's a slight refinement (replace array_search() with current()) sandbox.onlinephpfunctions.com/code/…
2

This should work...

function removeduplicate($myarray,$needle) {

    if (($nmatches = count($matches = array_keys($myarray,$needle))) >= 2) {

        for ($i=0; $i<$nmatches; $i++) {
            if ($matches[$i+1] == (1+$matches[$i])) {
                array_splice($myarray,$matches[$i],1);
                break;
            }
        }
    }

    return $myarray;
}

To use the function...

$arr1 = [1,2,2,3,3,3,4,7,3,3];
$newarray = removeduplicate($arr1,3)
print_r($newarray);

EDITED:

If you want the function to modify your original array directly, you can do it (the function will return true if a character has been removed or false if not)...

function removeduplicate(&$myarray,$needle) {

    if (($nmatches = count($matches = array_keys($myarray,$needle))) >= 2) {

        for ($i=0; $i<$nmatches; $i++) {
            if ($matches[$i+1] == (1+$matches[$i])) {
                array_splice($myarray,$matches[$i],1);
                return true;
            }
        }
    }

    return false;
}

So, now you can do...

$arr1 = [1,2,2,2,2,2,3,3,3,4,2,2];
removeduplicate($arr1,2);
print_r($arr1);

2 Comments

I think if your make the first if-condition as a while loop, you can actually match more then one duplicate number.
I've changed the approach. Thanks @Sysix!
0

This function will copy the array skipping only the second instance of the specified element.

function removeOnce($array, $elem) {
    $found = 0;
    $result = [];
    foreach ($array as $x) {
        if ($x == $elem && (++$found) == 2) {
            continue;
        }
       $result[] = $x;
    }
    return $result;
}

1 Comment

Unfortunately this method will do a lot of iterating to build the new array. A better / more efficient method would be to remove/unset one duplicate and break from the loop.

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.