Skip to content

Instantly share code, notes, and snippets.

@mavelar
Created November 23, 2019 05:53
Show Gist options
  • Select an option

  • Save mavelar/013b847531907faf99a19e1ee117a62c to your computer and use it in GitHub Desktop.

Select an option

Save mavelar/013b847531907faf99a19e1ee117a62c to your computer and use it in GitHub Desktop.
Hackerrank Can You Sort Test
<?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