I don’t think what to add to this code so that it works on the output of only unique values ​​(ie 3, 0)

<?php $arr = array(2, 5, 5, 3, 2, 10, 10, 0); for ($i = 0; $i < count($arr); $i++ ) { $a = true; for($j = 0; $j < $i; $j++){ if($arr[$i] == $arr[$j]){ $a = false; } } if($a){ echo $arr[$i]. " "; } } ?> 

And now all elements are displayed in the singular (ie, 2, 5, 3, 10, 0)

  • again without using built-in functions? :) - Alexey Shimansky
  • @ Alexey Shimansky, exactly xD - Yuri
  • Of course, Alexey)). But it would be cool if both with built-in and without showed) - Beginner
  • Well, using the functions you need here. stackoverflow.com/questions/527844/… .......... without functions a little differently - Alexey Shimansky

1 answer 1

Here is an option without built-in functions:

 <?php $arr = array(2, 5, 5, 3, 2, 10, 10, 0); for ($i = 0; $i < count($arr); $i++ ) { $a = true; $n = 0; for($j = 0; $j < count($arr); $j++){ if($arr[$i] == $arr[$j]){ $n = $n + 1; if($n > 1){ $a = false; } } } if($a){ echo $arr[$i]. " "; } } ?> 
  • and ... that's how it is ... cool .. just add a variable and make a condition with it ... I get it. Thank! - Beginner
  • eh ... the decision for the square, I was hoping for n log n at least) - pavel
  • @pavel, misunderstood) - Yuri
  • @Yuri well then the complexity of solving O (n ^ 2) I thought O (n log n) would be the answer or even O (n). - pavel
  • @pavel, mda .. :) - Yuri