skip to Main Content
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));

4

Answers


  1. I believe if I can get your question correct, you want to get the maximum occurring number of a given array, say number 4 appeared more than 3 times than any other number.

    Here is how you can achieve that:

    I added a lot of comments to help you understand how I solved it.

    
    
    
    let number = [1, 2, 3, 4, 4, 4, 5]
    
    let obj = {}
    
    function maxNumOccurence(num) {
       if (num.length === 0) {
            // return null for an empty array
           return null; 
       }
    
       /** `occurenceNum` object created to keep track of the count of each element */  
       const occurenceNum = {};
       let maxNum = num[0];
       let maxCount = 1;
    
       /**  We iterate through the array, `num`, updating the count for each element */  
       for (let i = 0; i < num.length; i++) {
        const number = num[i]
    
        // if iterated number is undefined, return one
        if(occurenceNum[number] === undefined) {
            occurenceNum[number] = 1;
        } else {
            occurenceNum[number]++;
        }
        // If we find an element with a higher count, maxCount, we update
        if (occurenceNum[number] > maxCount) {
            maxNum = number;
            // console.log(maxNum) => {4, 4, 4}
            maxCount = occurenceNum[number];
        }
       }
       return maxNum
    }
    
    
    // console.log the function with any array parameter of numbers, will return the max occurence count of the said array.
    
    console.log(maxNumOccurence(number))
    
    
    Login or Signup to reply.
  2. You could do it like that:

    const arr = [1, 2, 3, 4, 4, 4, 5];
    
    function findMaxFreq(arr){
      let counts = {}, max = null, maxCount = 0;
      for(const n of arr){
        const count = n in counts ? ++counts[n] : counts[n] = 1;
        if(count > maxCount) max = n, maxCount = count;
      }
      return max;
    }
    
    console.log(findMaxFreq(arr));

    If the input array is sorted:

    const arr = [1, 2, 3, 4, 4, 4, 5];
    
    let count = 1, n = arr[0], maxCount = 1, max = n;
    for(let i = 1; i < arr.length; i++){
      if(arr[i] !== n){
        if(count > maxCount) max = n, maxCount = count;
        count = 1, n = arr[i];
        if(maxCount > arr.length - i) break; // no more elements to beat the max
      } else {
        count++;
      }
    }
    console.log(max);
    Login or Signup to reply.
  3. you can use cache technique in this case like this:

    let arr1 = [1, 2, 3, 4, 4, 4, 5];
    let arr1Obj = {};
    
    function findMaxFreq(arr, obj){
      const cacheObj = arr.reduce((prev, next)=>{
        if (prev[next]) {
            prev[next]++
            return prev;
        }else{
            prev[next] = 1;
            return prev;
        }
      }, Obj);
      let highestFreq = 0;
      for(key in cacheObj){
         if(cacheObj[key] >= highestFreq){
               highestFreq = +key
         }
       }
      return highestFreq;
    }
    
    Login or Signup to reply.
  4. You could take a single loop with an array for collecting max frequency values, because of the possibillity of having two or more values with same frequency.

    At then end return either a single value, if only one value is available or an array of values.

    function findMaxFreq(array) {
        const counts = {};
        let max;
        for (const value of array) {
            counts[value] = (counts[value] || 0) + 1;
            if (!max || counts[value] > counts[max[0]]) {
                max = [value];
                continue;
            }
            if (counts[value] === counts[max[0]] && !max.includes(value)) max.push(value);
        }
        return max.length === 1
            ? max[0]
            : max;
    }
    
    const
        values = [1, 2, 3, 4, 4, 4, 5];
    
    console.log(findMaxFreq(values));
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search