How to remove duplicate pairs from two arrays?

I have two arrays:

$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 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 several repeating pairs, such as 1-7 and 7-1, 2-6 and 6-2, 3-5 and 5-3, 4-8 and 8-4.

I need a function that returns these two arrays and returns one array with each individual pair.

For example, this is what the function should return:

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

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

I made this function that does not 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 that:

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

What do you think about?

+4
source share
5 answers

After some corrections in the original script, this works:

 $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 ) 
+1
source

I think the most internal if it is wrong:

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

$ok rewritten every iteration; $ok will always matter, as if you just dropped the loop and set $j=count($arr2)-1 This may not be right.

Other problems may arise ...

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

  • It seems that it will be easy to change in order to accidentally get one array for one, and this infringes on each pair. You can switch to $arr[i][0] and $arr[i][1] . Thus, its impossible to accidentally make a mistake.
  • If the order doesn't matter in your pairs, it can be very helpful to maintain the invariant that pโ‚€ โ‰ค pโ‚. If you did this, for example, removing duplicates is very simple (and more efficient) by sorting.
  • Depending on your performance requirements and taking into account the invariant pโ‚€ โ‰ค p, you can simply save each pair as a string "pโ‚€,pโ‚" . Then standard (and even built-in) sortings and unique functions will work without any problems.
0
source

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); 
0
source

Try the following:

 $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.

0
source

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))); 
0
source

All Articles