Sorting a set of objects by a user's preference

前端 未结 10 1905
终归单人心
终归单人心 2020-12-13 16:09

Given a set of n objects in no specific order (n = 5 in this example):

{
    apple,
    orange,
    banana,
    cherry,
    cabbage
}

10条回答
  •  醉酒成梦
    2020-12-13 16:26

    Use a map (javascript object can act so) with object as the key and integer (set to 0 initially for all objects) as the value.

    For each question, you increment the value of the respective chosen key. Since no preference affects none of them, nothing to be done in case it's chosen.

    Then iterate the map, finding the key-value pair with highest value. Return the key.

提交回复
热议问题