How to prevent automatic sort of Object numeric property?

后端 未结 10 1258
醉酒成梦
醉酒成梦 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:35

    This is an old topic but it is still worth mentioning as it is hard to find a straight explanation in one-minute googling.

    I recently had a coding exercise that finding the first occurrence of the least/most frequent integer in an array, it is pretty much the same as your case.

    I encountered the same problem as you, having the numeric keys sorted by ASC in JavaScript object, which is not preserving the original order of elements, which is the default behavior in js.

    A better way to solve this in ES6 is to use a new data type called: Map

    Map can preserve the original order of elements(pairs), and also have the unique key benefit from object.

    let map = new Map()
    map.set(4, "first") // Map(1) {4 => "first"}
    map.set(1, "second") // Map(2) {4 => "first", 1 => "second"}
    map.set(2, "third") // Map(3) {4 => "first", 1 => "second", 2 => "third"}
    for(let [key, value] of map) {
      console.log(key, value)
    }
    // 4 "first"
    // 1 "second"
    // 2 "third"
    

    However, using the object data type can also solve the problem, but we need the help of the input array to get back the original order of elements:

    function findMostAndLeast(arr) {
      let countsMap = {};
      let mostFreq = 0;
      let leastFreq = arr.length;
      let mostFreqEl, leastFreqEl;
    
      for (let i = 0; i < arr.length; i++) {
        let el = arr[i];
        // Count each occurrence
        if (countsMap[el] === undefined) {
          countsMap[el] = 1;
        } else {
          countsMap[el] += 1;
        }
      }
    
      // Since the object is sorted by keys by default in JS, have to loop again the original array
      for (let i = 0; i < arr.length; i++) {
        const el = arr[i];
    
        // find the least frequent 
        if (leastFreq > countsMap[el]) {
          leastFreqEl = Number(el);
          leastFreq = countsMap[el];
        }
    
        // find the most frequent 
        if (countsMap[el] > mostFreq) {
          mostFreqEl = Number(el);
          mostFreq = countsMap[el];
        }
      }
    
      return {
        most_frequent: mostFreqEl,
        least_frequent: leastFreqEl
      }
    }
    const testData = [6, 1, 3, 2, 4, 7, 8, 9, 10, 4, 4, 4, 10, 1, 1, 1, 1, 6, 6, 6, 6];    
    console.log(findMostAndLeast(testData)); // { most_frequent: 6, least_frequent: 3 }, it gets 6, 3 instead of 1, 2 
    

提交回复
热议问题