How to find the property which has the highest value

  Kiến thức lập trình
let obj1 = { 1:1,2:1,4:3};

I want to get the property 4 as the output because the value of that property is highest of all other properties. I am trying to find out the highest frequency number in my own way.

let arr1 = [1, 2, 3, 4, 4, 4, 5];
let arr1Obj = {};

function findMaxFreq(arr, arr1Obj) {
  let freqArr = [];
  let objarrvalue = 0;
  for (let i = 0; i < arr.length; i++) {
    let frequencyOf = arr[i];
    let count = 1;
    for (let j = arr[i]; j < arr.length; j++) {
      if (arr[i] === arr[j]) {
        count++;
      } else {
        break;
      }
    }
    arr1Obj[arr[i]] = count;
  }
  let objvalues = Object.values(arr1Obj);
  for (let i = 0; i < objvalues.length; i++) {
    if (objvalues[i] > objarrvalue) {
      objarrvalue = objvalues[i];
    }
  }
  return objarrvalue;
}

console.log(findMaxFreq(arr1, arr1Obj));

New contributor

Ashuthosh Kanathala is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT