Newgen
/*
find the max occurence of element in arr
output =>{
3:4
}
*/
const arr =[1,2,2,2,,4,5,3,3,3,3];
function maxOccurance(arr){
let hashMap={}
let maxOccuranceKey=0;
let ans={}
for(let el of arr){
if(hashMap[el] !==undefined){
hashMap[el]+=1;
maxOccuranceKey = Math.max(maxOccuranceKey,hashMap[el])
}
else{
hashMap[el]=1;
}
}
Object.entries(hashMap).forEach(([key,val])=>{
if(val===maxOccuranceKey){
ans[key]=val;
}
})
return ans;
}
const res= maxOccurance(arr);
console.log(res)
// const optimized=useCallback(()=>{
// },[])
Comments
Post a Comment