Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

1,2,3,1,1,5,9

// 1
const givenArray = [1, 2, 3, 4, 1, 1, 2, 3, 4];

// 2
let itemsMap = {};
let maxValue = 0;
let maxCount = 0;

// 3
for (let item of givenArray) {
  // 4  
  if (itemsMap[item] == null) {
    itemsMap[item] = 1;
  } else {
    itemsMap[item]++;
  }

  //5
  if (itemsMap[item] > maxCount) {
    maxValue = item;
    maxCount = itemsMap[item];
  }
}

// 6
console.log(`Value : ${maxValue}, Count : ${maxCount}`);
Source by www.codevscolor.com #
 
PREVIOUS NEXT
Tagged:
ADD COMMENT
Topic
Name
5+7 =