Web Projects Consulting

Dominator Demo Task at Codility.com

codility netbeansLesson 6 – Leader – Dominator demo task solution code written in php. Gives 100/100 score at the time of publishing.

The description of the problem is copyrighted, so please see the following link for it: //codility.com/demo/take-sample-test/dominator

Solution in php:

function solution($A) {
    if(empty($A)){
        return -1;
    }
    $B = array_count_values($A);
    arsort($B, SORT_NUMERIC);
    list($k, $v) = each($B);
    $dom = ($v > count($A)/2) ? $k : null;
    if(is_null($dom)){
        return -1;
    }
    foreach($A as $k => &$v){
        if($v == $dom){
            return $k;
        }
    }
}

Given “AS IS”, can be ported from other languages from solutions found on the internet, please use with care.

Please note: we think that codility.com does not give a correct assessment of your real-world programming skills. For instance, reading the below would bring more understanding why: //codility-test-questions.blogspot.com/2013/01/my-experience-with-codility-test.html

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.