gaggina
gaggina

Reputation: 5425

how to delete duplicate pairs from two array?

I've two arrays like this:

$arr = Array (1, 2, 3 ,4 ,5, 6 ,7 ,8 ) ;

and this:

$arr2 = Array (7, 6, 5,8 ,3 ,2 ,1, 4 )  

The pairs of these arrays are the numbers with the same key ($arr[0]-$arr2[0] ecc.)

1-7
2-6
3-5
4-8
5-3
6-2
7-1
8-4

as you can see there are some duplicate pairs like 1-7 and 7-1 , 2-6 and 6-2 , 3-5 and 5-3, 4-8 and 8-4.

I need a function who recives these two arrays and return one array with each single pairs.

For example this is what the function should returns:

Array ( [0] => 1 [1] => 7 [2] => 2 [3] => 6 [4] => 3 [5] => 5 [6] => 4 [7] => 8 ) 

As you can see the pairs ares: 1-7 , 2-6, 3-5 and 4-8.

I've made this function who doesnt work properly:

function free_pairs($arr,$arr2){
$ok = 0;
$ris = array();
$indice_ris=0;
for ($i=1; $i <=count($arr) ; $i++) { 
    $x1 = $arr[$i];
    $x2 = $arr2[$i];
    for ($j=1; $j <= count($arr2) ; $j++) { 
        $y1 = $arr[$j];
        $y2 = $arr2[$j];
        if($x1 != $y2 && $x2 != $y1){
            $ok = 1;
        } else {
            $ok = 0;
        }
    }
    if ($ok == 1) {
        $ris[$indice_ris] = $x1;
        $ris[$indice_ris+1] = $x2;
        $indice_ris = $indice_ris+2;
        $ok = 0;
    }
    return $ris;
}

I think the problem is in this if:

if($x1 != $y2 && $x2 !=$y1)

What do you think about?

Upvotes: 4

Views: 448

Answers (5)

JRL
JRL

Reputation: 78003

An oo approach:

class Pair {
    private $x;
    private $y;

    public function __construct($a, $b) {
        $this->x = ($a > $b ? $a : $b);
        $this->y = ($a > $b ? $b : $a);
    }

    public function __toString() {
        return "{$this->x}, {$this->y}";
    }

    public static function uniquePairs($arr1, $arr2) {
        $pairs = array();
        foreach(array_combine($arr1, $arr2) as $key => $val) {
            $pair = new Pair($key, $val);
            $pairs[(string)$pair] = $pair;
        }
        return $pairs;
    }
}

// usage:
var_dump(Pair::uniquePairs(array(1,2,3,4,5,6,7,8), array(7,6,5,8,3,2,1,4)));

Upvotes: 0

Vikk
Vikk

Reputation: 3363

Try this:

$arr1 = array (1, 2, 3 ,4 ,5, 6 ,7 ,8 ) ;
$arr2 = array (7, 6, 5,8 ,3 ,2 ,1, 4 ) ; 

$arr3 = array();

$pairs = array();

for($i =0;$i<count($arr1);$i++)
{
    $pair1 = $arr1[$i].'-'.$arr2[$i];
    $pair2 = $arr2[$i].'-'.$arr1[$i];

    if(!isset($pairs[$pair1]) && !isset($pairs[$pair2]))
    {
        $arr3[] = $arr1[$i];
        $arr3[] = $arr2[$i];

        $pairs[$pair1] = true;
    }
}

$arr3 is your final array.

Upvotes: 0

vicentazo
vicentazo

Reputation: 1799

Another way:

$arr1 = array(1, 2, 3, 4, 5, 6, 7, 8);
$arr2 = array(7, 6, 5, 8, 3, 2, 1, 4);

function concat(&$item, $key, $arr)
{
    $item2 = $arr[$key];
    if($item < $item2)
        $item .= "-" . $item2;
    else
        $item = $item2 . "-" . $item;
}

array_walk($arr1, 'concat', $arr2);
print_r($arr1);

$arr = array_unique($arr1);
print_r($arr);

Upvotes: 0

Toto
Toto

Reputation: 91430

After some corrections on your original script, this seems to work:

$arr1 = Array (1, 2, 3, 4 ,5, 6 ,7 ,8);
$arr2 = Array (7, 6, 5, 8 ,3 ,2 ,1, 4);
$res = free_pairs($arr1, $arr2);
print_r($res);

function free_pairs($arr,$arr2){
    $ris = array();
    for ($i = 0; $i < count($arr); $i++) { 
        $x1 = $arr[$i];
        $x2 = $arr2[$i];
        $ok = 0;
        for ($j = $i+1; $j < count($arr2); $j++) { 
            $y1 = $arr[$j];
            $y2 = $arr2[$j];
            if($x1 == $y2 && $x2 == $y1){
                $ok = 1;
            }
        }
        if ($ok == 0) {
            $ris[] = $x1;
            $ris[] = $x2;
        }
    }
    return $ris;
}

output:

Array
(
    [0] => 5
    [1] => 3
    [2] => 6
    [3] => 2
    [4] => 7
    [5] => 1
    [6] => 8
    [7] => 4
)

Upvotes: 1

derobert
derobert

Reputation: 51157

I think the innermost if is wrong:

        if($x1 != $y2 && $x2 !=$y1){
            $ok = 1;
        }
        else{
            $ok=0;
        }

$ok is overwritten each iteration; $ok will always have the value as if you'd just omitted the loop and set $j=count($arr2)-1 This can't possibly be right.

There may be other problems...

I also note that your data structures are iffy. A couple of things:

  • Looks like it'll be vary easy to accidentally get one array off by one, and this mispair each pair. You may want to switch to $arr[i][0] and $arr[i][1] being the pair. This way, its impossible to accidentally mispair.
  • If order doesn't matter in your pairs, it can be very useful to maintain the invariant that p₀ ≤ p₁. If you did that, for example, removing duplicates is very easy (and more efficient) by sorting.
  • Depending on your performance requirements and given a p₀ ≤ p₁ invariant, you may be able to just store each pair as a string "p₀,p₁". Then standard (and even built-in) sort and unique functions will work without any trouble.

Upvotes: 0

Related Questions