-1

As you will notice, I'm trying to analyze which value will occur the most frequent within a given array using hashmaps and objects. If you can, please try to identify the answer for each algorithm (hashmap and object).

The below example uses hashmaps to "map" various objects with each other. However, I am having two problems: 1. I can't increment occurrence properly: after occurrence variable is equal to 1, then that value remains forever hence the algorithm provides the wrong mode. 2. I don't know how to change the value of a map after the iteration is completed.

var highestFrequency = (array) => {
  var occurenceStored = new Map();
  let occurrence = 0;
  for (var i = 0; i < array.length; i++){
    var j = i + 1;
    if (array[i] === array[j]){
      occurrence += 1;
      occurenceStored.set(array[i], occurrence)
    }
  }

  console.log(occurenceStored);
}


highestFrequency([101, 104, 108, 108, 111, 119, 119, 119])

Instead of using a hashmap, I decided to use an object instead but I fall into the same problem. The occurrence variable is permanently equal to 1. I tried declaring the occurrence variable at different places but can't seem to find the right fix.

var highestFrequency = (array) => {
  var occurenceStored = {};
  let occurrence = 1;
  for (var i = 0; i < array.length; i++){
    var j = i + 1;
    if (array[i] === array[j]){
      occurenceStored[array[i]] = occurrence;
    }
  }

  console.log(occurenceStored);
}


highestFrequency([101, 104, 108, 108, 111, 119, 119, 119, 120, 120, 120])
Shreejal Luitel
  • 135
  • 1
  • 7
  • 1
    how about doing that with `reduce` method? `array.reduce((a, e)=>(a[e] = a[e] || 0, a[e]++, a),{})` – gorak Jun 21 '20 at 05:32

3 Answers3

2

If you need to use the Map, then you can do in this way:

const highestFreq = (array) => {
  let myMap = new Map();
  for( let i = 0; i < array.length; i++ ) {
    if (myMap[array[i]]) myMap[array[i]]++;
    else myMap[array[i]] = 1; 
  }
  return myMap;
}

const myArray = [101, 104, 108, 108, 111, 119, 119, 119];

const myMap = highestFreq(myArray);
console.log(myMap); 

myMap contains the key value pair, where key are the unique elements from the given array and the mapped values are their occurences.

Mateen
  • 1,455
  • 10
  • 17
2

Using Object way , you can achieve like below:

var highestFrequency = (array) => {
  var occurenceStored = {};
  for (var i = 0; i < array.length; i++){
    
    if (occurenceStored[array[i]]){
      occurenceStored[array[i]] = occurenceStored[array[i]]+1;
    } else{
        occurenceStored[array[i]]=1;
    }
  }

  console.log(occurenceStored);
}
Harmandeep Singh Kalsi
  • 3,315
  • 2
  • 14
  • 26
1

assuming you only want the most frequent value (what if there are two different values with the same frequency ?):

  • increment a counter per value
  • when iterating, if a counter is greater thatn your biggest counter, make it the biggest instead.
const highestFreq = arr => {
  const best = { value: 0, n: 0 } // what to do if empty array ??
  arr.reduce((m, el) => {
    const counter = (m.get(el) || 0) + 1
    if (counter > best.n) {
      best.value = el
      best.n = counter
    }
    return m.set(el, counter)
  }, new Map())
  return best // or compute your frequency: best.n / arr.length
}

console.log(highestFreq([101, 104, 108, 108, 111, 119, 119, 119]))
grodzi
  • 5,633
  • 1
  • 15
  • 15