Created
November 23, 2019 05:53
-
-
Save mavelar/013b847531907faf99a19e1ee117a62c to your computer and use it in GitHub Desktop.
Hackerrank Can You Sort Test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function customSort($arr) { | |
| $keys = array_count_values($arr); | |
| $newsort = []; | |
| $conc = []; | |
| $output = ""; | |
| asort($keys); | |
| foreach($keys as $k => $v) { | |
| if($v>1) { | |
| for($i=0; $i < $v; $i++) { | |
| $conc[] = $k; | |
| } | |
| } else { | |
| $newsort[] = $k; | |
| } | |
| } | |
| sort($newsort); | |
| $final = array_merge($newsort,$conc); | |
| foreach($final as $i) { | |
| fwrite(STDOUT, "{$i}\n"); | |
| } | |
| } | |
| print_r(customSort([10,8,5,5,5,5,1,1,1,4,4])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment