<html> <head><title>504 Gateway Time-out</title></head> <body> <center><h1>504 Gateway Time-out</h1></center> <hr><center>nginx</center> </body> </html>

how does js in array find out the most repeated elements in the array (try to use syntax above es6, be concise)

let ary = ["", "", "", "", "", "", "", "", ""];

add: 2018-7-17 10:42:53
in fact, I started with an exercise on arrays with mockjs, and now this problem is the requirement part of func10, demo= >
https://codepen.io/vizocn/pen.

.

func10 is not finished yet. I"d like to ask you if there is a better and more concise way to solve it.
the initial array data structure is shown in the following figure

Mar.28,2021

function search(arr){
    var maxCount = 0,
        maxItem = '',
        obj = {}
    arr.forEach(function(item){
        obj[item] ? (obj[item].count += 1) : obj[item] = {count: 1}
        obj[item].count > maxCount && (maxCount = obj[item].count, maxItem = item)
    })
    //return {item:maxItem,count:maxCount}
    return maxItem
}

const d = {};
let ary = ['', '', '', '', '', '', '', '', ''];
ary.forEach(k => !d[k] ? d[k] = 1 : d[k]PP);
const max = Object.keys(d).sort((a, b) => d[b] - d[a])[0];
console.log(max)

two lines is concise enough if you don't define a variable.

const d = {};
let ary = ['', '', '', '', '', '', '', '', '', ''];
ary.forEach(k => !d[k] ? d[k] = 1 : d[k]PP);
const result = Object.keys(d).sort((a, b) => d[b] - d[a]).filter((k, i, l) => d[k] === d[l[0]]);
console.log(result)

updated result is now the array result the longest number of values will be listed.

if you only take the maximum value, it is recommended to use the method of hfhan , which is more orthodox. This is . the code looks short and actually has 2 more loops, but if you have the need to sort the number of repetitions from large to small, you can use

.

updated the case that there may be more than one

  // map
  // 
  function findMaxRepeatedElement(arr) {
    if (!Array.isArray(arr)) {
      return;
    }
    if (arr.length === 1) {
      return arr[1];
    }
    //  map
    const map = new Map();
    const maxCount = arr.reduce((maxCount, item) => {
      map.set(item, map.get(item) ? map.get(item) + 1 : 1);
      return map.get(item) > maxCount ? map.get(item) : maxCount;
    }, 0);
    // 
    return [...map].filter((item) => item[1] && item[1] === maxCount);
  }
Menu