Skip to content

Instantly share code, notes, and snippets.

@anujsinghwd
Created November 17, 2018 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anujsinghwd/34056c0d14d63cba13456ed5a4106d0d to your computer and use it in GitHub Desktop.
Save anujsinghwd/34056c0d14d63cba13456ed5a4106d0d to your computer and use it in GitHub Desktop.
Sort an array of 0s, 1s and 2s
<?php
$arr = array(0,2,1,2,0);
$n = count($arr);
$count = array_count_values($arr);
$new_arr;
for($j=0;$j<=2;$j++){
for($i=0;$i<$count[$j];$i++){
$new_arr[] = $j;
}
}
print_r($new_arr);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment