3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function &permuteUnique($items) { sort($items); $size = count($items); $return = array(); while (true) { $return[] = $items; $invAt = $size - 2; for (;;$invAt--) { if ($invAt < 0) { break 2; } if ($items[$invAt] < $items[$invAt + 1]) { break; } } $swap1Num = $items[$invAt]; $inv2At = $size - 1; while ($swap1Num >= $items[$inv2At]) { $inv2At--; } $items[$invAt] = $items[$inv2At]; $items[$inv2At] = $swap1Num; $reverse1 = $invAt + 1; $reverse2 = $size - 1; while ($reverse1 < $reverse2) { $temp = $items[$reverse1]; $items[$reverse1] = $items[$reverse2]; $items[$reverse2] = $temp; $reverse1++; $reverse2--; } } return $return; } var_dump(permuteUnique(array(1,2,3,4,5))) ?>
Output for 4.3.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
array(120) { [0]=> array(5) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) } [1]=> array(5) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(5) [4]=> int(4) } [2]=> array(5) { [0]=> int(1) [1]=> int(2) [2]=> int(4) [3]=> int(3) [4]=> int(5) } [3]=> array(5) { [0]=> int(1) [1]=> int(2) [2]=> int(4) [3]=> int(5) [4]=> int(3) } [4]=> array(5) { [0]=> int(1) [1]=> int(2) [2]=> int(5) [3]=> int(3) [4]=> int(4) } [5]=> array(5) { [0]=> int(1) [1]=> int(2) [2]=> int(5) [3]=> int(4) [4]=> int(3) } [6]=> array(5) { [0]=> int(1) [1]=> int(3) [2]=> int(2) [3]=> int(4) [4]=> int(5) } [7]=> array(5) { [0]=> int(1) [1]=> int(3) [2]=> int(2) [3]=> int(5) [4]=> int(4) } [8]=> array(5) { [0]=> int(1) [1]=> int(3) [2]=> int(4) [3]=> int(2) [4]=> int(5) } [9]=> array(5) { [0]=> int(1) [1]=> int(3) [2]=> int(4) [3]=> int(5) [4]=> int(2) } [10]=> array(5) { [0]=> int(1) [1]=> int(3) [2]=> int(5) [3]=> int(2) [4]=> int(4) } [11]=> array(5) { [0]=> int(1) [1]=> int(3) [2]=> int(5) [3]=> int(4) [4]=> int(2) } [12]=> array(5) { [0]=> int(1) [1]=> int(4) [2]=> int(2) [3]=> int(3) [4]=> int(5) } [13]=> array(5) { [0]=> int(1) [1]=> int(4) [2]=> int(2) [3]=> int(5) [4]=> int(3) } [14]=> array(5) { [0]=> int(1) [1]=> int(4) [2]=> int(3) [3]=> int(2) [4]=> int(5) } [15]=> array(5) { [0]=> int(1) [1]=> int(4) [2]=> int(3) [3]=> int(5) [4]=> int(2) } [16]=> array(5) { [0]=> int(1) [1]=> int(4) [2]=> int(5) [3]=> int(2) [4]=> int(3) } [17]=> array(5) { [0]=> int(1) [1]=> int(4) [2]=> int(5) [3]=> int(3) [4]=> int(2) } [18]=> array(5) { [0]=> int(1) [1]=> int(5) [2]=> int(2) [3]=> int(3) [4]=> int(4) } [19]=> array(5) { [0]=> int(1) [1]=> int(5) [2]=> int(2) [3]=> int(4) [4]=> int(3) } [20]=> array(5) { [0]=> int(1) [1]=> int(5) [2]=> int(3) [3]=> int(2) [4]=> int(4) } [21]=> array(5) { [0]=> int(1) [1]=> int(5) [2]=> int(3) [3]=> int(4) [4]=> int(2) } [22]=> array(5) { [0]=> int(1) [1]=> int(5) [2]=> int(4) [3]=> int(2) [4]=> int(3) } [23]=> array(5) { [0]=> int(1) [1]=> int(5) [2]=> int(4) [3]=> int(3) [4]=> int(2) } [24]=> array(5) { [0]=> int(2) [1]=> int(1) [2]=> int(3) [3]=> int(4) [4]=> int(5) } [25]=> array(5) { [0]=> int(2) [1]=> int(1) [2]=> int(3) [3]=> int(5) [4]=> int(4) } [26]=> array(5) { [0]=> int(2) [1]=> int(1) [2]=> int(4) [3]=> int(3) [4]=> int(5) } [27]=> array(5) { [0]=> int(2) [1]=> int(1) [2]=> int(4) [3]=> int(5) [4]=> int(3) } [28]=> array(5) { [0]=> int(2) [1]=> int(1) [2]=> int(5) [3]=> int(3) [4]=> int(4) } [29]=> array(5) { [0]=> int(2) [1]=> int(1) [2]=> int(5) [3]=> int(4) [4]=> int(3) } [30]=> array(5) { [0]=> int(2) [1]=> int(3) [2]=> int(1) [3]=> int(4) [4]=> int(5) } [31]=> array(5) { [0]=> int(2) [1]=> int(3) [2]=> int(1) [3]=> int(5) [4]=> int(4) } [32]=> array(5) { [0]=> int(2) [1]=> int(3) [2]=> int(4) [3]=> int(1) [4]=> int(5) } [33]=> array(5) { [0]=> int(2) [1]=> int(3) [2]=> int(4) [3]=> int(5) [4]=> int(1) } [34]=> array(5) { [0]=> int(2) [1]=> int(3) [2]=> int(5) [3]=> int(1) [4]=> int(4) } [35]=> array(5) { [0]=> int(2) [1]=> int(3) [2]=> int(5) [3]=> int(4) [4]=> int(1) } [36]=> array(5) { [0]=> int(2) [1]=> int(4) [2]=> int(1) [3]=> int(3) [4]=> int(5) } [37]=> array(5) { [0]=> int(2) [1]=> int(4) [2]=> int(1) [3]=> int(5) [4]=> int(3) } [38]=> array(5) { [0]=> int(2) [1]=> int(4) [2]=> int(3) [3]=> int(1) [4]=> int(5) } [39]=> array(5) { [0]=> int(2) [1]=> int(4) [2]=> int(3) [3]=> int(5) [4]=> int(1) } [40]=> array(5) { [0]=> int(2) [1]=> int(4) [2]=> int(5) [3]=> int(1) [4]=> int(3) } [41]=> array(5) { [0]=> int(2) [1]=> int(4) [2]=> int(5) [3]=> int(3) [4]=> int(1) } [42]=> array(5) { [0]=> int(2) [1]=> int(5) [2]=> int(1) [3]=> int(3) [4]=> int(4) } [43]=> array(5) { [0]=> int(2) [1]=> int(5) [2]=> int(1) [3]=> int(4) [4]=> int(3) } [44]=> array(5) { [0]=> int(2) [1]=> int(5) [2]=> int(3) [3]=> int(1) [4]=> int(4) } [45]=> array(5) { [0]=> int(2) [1]=> int(5) [2]=> int(3) [3]=> int(4) [4]=> int(1) } [46]=> array(5) { [0]=> int(2) [1]=> int(5) [2]=> int(4) [3]=> int(1) [4]=> int(3) } [47]=> array(5) { [0]=> int(2) [1]=> int(5) [2]=> int(4) [3]=> int(3) [4]=> int(1) } [48]=> array(5) { [0]=> int(3) [1]=> int(1) [2]=> int(2) [3]=> int(4) [4]=> int(5) } [49]=> array(5) { [0]=> int(3) [1]=> int(1) [2]=> int(2) [3]=> int(5) [4]=> int(4) } [50]=> array(5) { [0]=> int(3) [1]=> int(1) [2]=> int(4) [3]=> int(2) [4]=> int(5) } [51]=> array(5) { [0]=> int(3) [1]=> int(1) [2]=> int(4) [3]=> int(5) [4]=> int(2) } [52]=> array(5) { [0]=> int(3) [1]=> int(1) [2]=> int(5) [3]=> int(2) [4]=> int(4) } [53]=> array(5) { [0]=> int(3) [1]=> int(1) [2]=> int(5) [3]=> int(4) [4]=> int(2) } [54]=> array(5) { [0]=> int(3) [1]=> int(2) [2]=> int(1) [3]=> int(4) [4]=> int(5) } [55]=> array(5) { [0]=> int(3) [1]=> int(2) [2]=> int(1) [3]=> int(5) [4]=> int(4) } [56]=> array(5) { [0]=> int(3) [1]=> int(2) [2]=> int(4) [3]=> int(1) [4]=> int(5) } [57]=> array(5) { [0]=> int(3) [1]=> int(2) [2]=> int(4) [3]=> int(5) [4]=> int(1) } [58]=> array(5) { [0]=> int(3) [1]=> int(2) [2]=> int(5) [3]=> int(1) [4]=> int(4) } [59]=> array(5) { [0]=> int(3) [1]=> int(2) [2]=> int(5) [3]=> int(4) [4]=> int(1) } [60]=> array(5) { [0]=> int(3) [1]=> int(4) [2]=> int(1) [3]=> int(2) [4]=> int(5) } [61]=> array(5) { [0]=> int(3) [1]=> int(4) [2]=> int(1) [3]=> int(5) [4]=> int(2) } [62]=> array(5) { [0]=> int(3) [1]=> int(4) [2]=> int(2) [3]=> int(1) [4]=> int(5) } [63]=> array(5) { [0]=> int(3) [1]=> int(4) [2]=> int(2) [3]=> int(5) [4]=> int(1) } [64]=> array(5) { [0]=> int(3) [1]=> int(4) [2]=> int(5) [3]=> int(1) [4]=> int(2) } [65]=> array(5) { [0]=> int(3) [1]=> int(4) [2]=> int(5) [3]=> int(2) [4]=> int(1) } [66]=> array(5) { [0]=> int(3) [1]=> int(5) [2]=> int(1) [3]=> int(2) [4]=> int(4) } [67]=> array(5) { [0]=> int(3) [1]=> int(5) [2]=> int(1) [3]=> int(4) [4]=> int(2) } [68]=> array(5) { [0]=> int(3) [1]=> int(5) [2]=> int(2) [3]=> int(1) [4]=> int(4) } [69]=> array(5) { [0]=> int(3) [1]=> int(5) [2]=> int(2) [3]=> int(4) [4]=> int(1) } [70]=> array(5) { [0]=> int(3) [1]=> int(5) [2]=> int(4) [3]=> int(1) [4]=> int(2) } [71]=> array(5) { [0]=> int(3) [1]=> int(5) [2]=> int(4) [3]=> int(2) [4]=> int(1) } [72]=> array(5) { [0]=> int(4) [1]=> int(1) [2]=> int(2) [3]=> int(3) [4]=> int(5) } [73]=> array(5) { [0]=> int(4) [1]=> int(1) [2]=> int(2) [3]=> int(5) [4]=> int(3) } [74]=> array(5) { [0]=> int(4) [1]=> int(1) [2]=> int(3) [3]=> int(2) [4]=> int(5) } [75]=> array(5) { [0]=> int(4) [1]=> int(1) [2]=> int(3) [3]=> int(5) [4]=> int(2) } [76]=> array(5) { [0]=> int(4) [1]=> int(1) [2]=> int(5) [3]=> int(2) [4]=> int(3) } [77]=> array(5) { [0]=> int(4) [1]=> int(1) [2]=> int(5) [3]=> int(3) [4]=> int(2) } [78]=> array(5) { [0]=> int(4) [1]=> int(2) [2]=> int(1) [3]=> int(3) [4]=> int(5) } [79]=> array(5) { [0]=> int(4) [1]=> int(2) [2]=> int(1) [3]=> int(5) [4]=> int(3) } [80]=> array(5) { [0]=> int(4) [1]=> int(2) [2]=> int(3) [3]=> int(1) [4]=> int(5) } [81]=> array(5) { [0]=> int(4) [1]=> int(2) [2]=> int(3) [3]=> int(5) [4]=> int(1) } [82]=> array(5) { [0]=> int(4) [1]=> int(2) [2]=> int(5) [3]=> int(1) [4]=> int(3) } [83]=> array(5) { [0]=> int(4) [1]=> int(2) [2]=> int(5) [3]=> int(3) [4]=> int(1) } [84]=> array(5) { [0]=> int(4) [1]=> int(3) [2]=> int(1) [3]=> int(2) [4]=> int(5) } [85]=> array(5) { [0]=> int(4) [1]=> int(3) [2]=> int(1) [3]=> int(5) [4]=> int(2) } [86]=> array(5) { [0]=> int(4) [1]=> int(3) [2]=> int(2) [3]=> int(1) [4]=> int(5) } [87]=> array(5) { [0]=> int(4) [1]=> int(3) [2]=> int(2) [3]=> int(5) [4]=> int(1) } [88]=> array(5) { [0]=> int(4) [1]=> int(3) [2]=> int(5) [3]=> int(1) [4]=> int(2) } [89]=> array(5) { [0]=> int(4) [1]=> int(3) [2]=> int(5) [3]=> int(2) [4]=> int(1) } [90]=> array(5) { [0]=> int(4) [1]=> int(5) [2]=> int(1) [3]=> int(2) [4]=> int(3) } [91]=> array(5) { [0]=> int(4) [1]=> int(5) [2]=> int(1) [3]=> int(3) [4]=> int(2) } [92]=> array(5) { [0]=> int(4) [1]=> int(5) [2]=> int(2) [3]=> int(1) [4]=> int(3) } [93]=> array(5) { [0]=> int(4) [1]=> int(5) [2]=> int(2) [3]=> int(3) [4]=> int(1) } [94]=> array(5) { [0]=> int(4) [1]=> int(5) [2]=> int(3) [3]=> int(1) [4]=> int(2) } [95]=> array(5) { [0]=> int(4) [1]=> int(5) [2]=> int(3) [3]=> int(2) [4]=> int(1) } [96]=> array(5) { [0]=> int(5) [1]=> int(1) [2]=> int(2) [3]=> int(3) [4]=> int(4) } [97]=> array(5) { [0]=> int(5) [1]=> int(1) [2]=> int(2) [3]=> int(4) [4]=> int(3) } [98]=> array(5) { [0]=> int(5) [1]=> int(1) [2]=> int(3) [3]=> int(2) [4]=> int(4) } [99]=> array(5) { [0]=> int(5) [1]=> int(1) [2]=> int(3) [3]=> int(4) [4]=> int(2) } [100]=> array(5) { [0]=> int(5) [1]=> int(1) [2]=> int(4) [3]=> int(2) [4]=> int(3) } [101]=> array(5) { [0]=> int(5) [1]=> int(1) [2]=> int(4) [3]=> int(3) [4]=> int(2) } [102]=> array(5) { [0]=> int(5) [1]=> int(2) [2]=> int(1) [3]=> int(3) [4]=> int(4) } [103]=> array(5) { [0]=> int(5) [1]=> int(2) [2]=> int(1) [3]=> int(4) [4]=> int(3) } [104]=> array(5) { [0]=> int(5) [1]=> int(2) [2]=> int(3) [3]=> int(1) [4]=> int(4) } [105]=> array(5) { [0]=> int(5) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(1) } [106]=> array(5) { [0]=> int(5) [1]=> int(2) [2]=> int(4) [3]=> int(1) [4]=> int(3) } [107]=> array(5) { [0]=> int(5) [1]=> int(2) [2]=> int(4) [3]=> int(3) [4]=> int(1) } [108]=> array(5) { [0]=> int(5) [1]=> int(3) [2]=> int(1) [3]=> int(2) [4]=> int(4) } [109]=> array(5) { [0]=> int(5) [1]=> int(3) [2]=> int(1) [3]=> int(4) [4]=> int(2) } [110]=> array(5) { [0]=> int(5) [1]=> int(3) [2]=> int(2) [3]=> int(1) [4]=> int(4) } [111]=> array(5) { [0]=> int(5) [1]=> int(3) [2]=> int(2) [3]=> int(4) [4]=> int(1) } [112]=> array(5) { [0]=> int(5) [1]=> int(3) [2]=> int(4) [3]=> int(1) [4]=> int(2) } [113]=> array(5) { [0]=> int(5) [1]=> int(3) [2]=> int(4) [3]=> int(2) [4]=> int(1) } [114]=> array(5) { [0]=> int(5) [1]=> int(4) [2]=> int(1) [3]=> int(2) [4]=> int(3) } [115]=> array(5) { [0]=> int(5) [1]=> int(4) [2]=> int(1) [3]=> int(3) [4]=> int(2) } [116]=> array(5) { [0]=> int(5) [1]=> int(4) [2]=> int(2) [3]=> int(1) [4]=> int(3) } [117]=> array(5) { [0]=> int(5) [1]=> int(4) [2]=> int(2) [3]=> int(3) [4]=> int(1) } [118]=> array(5) { [0]=> int(5) [1]=> int(4) [2]=> int(3) [3]=> int(1) [4]=> int(2) } [119]=> array(5) { [0]=> int(5) [1]=> int(4) [2]=> int(3) [3]=> int(2) [4]=> int(1) } }