How to prevent automatic sort of Object numeric property?

后端 未结 10 1275
醉酒成梦
醉酒成梦 2020-12-03 20:57

Why I met this problem: I tried to solve an algorithm problem and I need to return the number which appeared most of the times in an array. Like [5,4,3,2,1,1] should return

10条回答
  •  生来不讨喜
    2020-12-03 21:44

    You are using a JS object, that by definition does not keep order. Think of it as a key => value map.

    You should be using an array, that will keep whatever you insert on the index you inserted it into. Think of it as a list.

    Also notice that you did not in fact "reverse the order of the assignment", because you inserted elements on the same index every time.

提交回复
热议问题