2

The result should be a 2 element array with the 2 number that are most frequent; I can return the top 2 most amount of times a number has been spotted (my //comments). But I can not figure out how to return the correct keys from the pairs object that appear the most times. Please help.

/* majorityElementTopTwo
 * Write a function which accept an array of integers and returns in a new two item array
 * the two integers in the input that appear most frequently.
 * majorityElementTopTwo([3,3,1,2,1,1,4,4,4,4]); // [4,1]
 */


function majorityElementTopTwo(array) {
  let result = [];
  let pairs = {};

  for (let i = 0; i < array.length; i++) {
    if (array[i] in pairs) {
      pairs[array[i]] += 1;
    } else {
      pairs[array[i]] = 1;
    }
  }
  return pairs;

  //  obj.sort();

  //  for(let i = obj.length; i > 0; i--){
  //    let num = obj.pop();
  //    console.log(num);
  //    result.push(num);
  //  }

  //  return result.slice(0,2);
}

console.log(majorityElementTopTwo([3, 3, 1, 2, 1, 1, 4, 4, 4, 4])); // [4,1]
Randy Casburn
  • 13,840
  • 1
  • 16
  • 31

3 Answers3

1

As of JavaScript you can use Object.keys(obj) to get an Array of properties defined on the object itself (the ones that return true for obj.hasOwnProperty(key)).

Object.keys(obj).forEach(function(key,index) {
    // key: the name of the object key
    // index: the ordinal position of the key within the object 
});

This is better (and more readable) than using a for-in loop.

Its supported on these browsers:

Firefox (Gecko): 4 (2.0) Chrome: 5 Internet Explorer: 9

ref: Iterate through object properties

Jakir Hossen
  • 451
  • 4
  • 13
1

I hope this is the answer you are looking for what I have done here is

  1. first map all the entries as an array ie: [["1",3],["2",1],["3",2],["4",4]]
  2. Then sort it based upon the second element which is number of times an element has occured
  3. Then slice the first two elements that are most occurred [["4",4],["1",3]]

/* majorityElementTopTwo
 * Write a function which accept an array of integers and returns in a new two item array
 * the two integers in the input that appear most frequently.
 * majorityElementTopTwo([3,3,1,2,1,1,4,4,4,4]); // [4,1]
 */


function majorityElementTopTwo(array) {

  let pairs = {};

  for (let i = 0; i < array.length; i++) {
    if (array[i] in pairs) {
      pairs[array[i]] += 1;
    } else {
      pairs[array[i]] = 1;
    }
  }

return Object.entries(pairs).map((elements) => elements).sort((a,b) => b[1]-a[1]).slice(0,2)

}

console.log(majorityElementTopTwo([3, 3, 1, 2, 1, 1, 4, 4, 4, 4])); // [4,1]
strek
  • 1,190
  • 1
  • 9
  • 19
0

Using Array.prototype.map(),Array.prototype.filter() and Array.prototype.sort().

/* majorityElementTopTwo
 * Write a function which accept an array of integers and returns in a new two item array
 * the two integers in the input that appear most frequently.
 * majorityElementTopTwo([3,3,1,2,1,1,4,4,4,4]); // [4,1]
 */

function majorityElementTopTwo(array) {
  const [first, second] = [...new Set(array)]
    .map((num) => [num, array.filter((n) => n === num).length])
    .sort((a, b) => b[1] - a[1])
    .slice(0, 2);
  return [first[0], second[0]];
}

console.log(majorityElementTopTwo([3, 3, 1, 2, 1, 1, 4, 4, 4, 4]));