You are on page 1of 3

find all elements that appear more than ⌊ n/3 ⌋ times.

where n is length of array

Input: [2,1,3,1,1,1]
Output: [1]

// list = []
// map => {key => elm, value, freq}
// map => {key > freq > n / 3}
// return [list]
// else return []

// code structure
var findElement = function(nums) {
let response = []
let hMap = new Map();

// making the freq map


nums.forEach((ele, idx) => {
if(hMap.has(ele)) {
hMap.set(ele, Number(hMap.get(ele)) + 1)
} else {
hMap.set(ele, 1)
}
})

let n = nums.length

// checking for freq. count


hMap.forEach((value, key) => {
if(value > n / 3) {
response.push(key)
}
})

return response;

};

nums = [2,1,3,1,1,1]

1. itration
hMap = {}
// ele = 2;
if hMAp is having the ele
hMap = {2: currentCount}
else
hMap = {2: 1}

... all the n count


n = length of the nums

hMap = {2: 1, 1: 4, 3: 1}

reponse = []

2. itration
if any hMap key is having a value > n / 2
add to reponse
// reponse.push(1)
else
// reponse is still empty

return reponse

Output: [1]

let obj = {
a : {
b : (a,b,c)=>a+b+c, // callback
c : (a,b,c) => a+b-c,
},
d : (a,b,c) => a-b-c
};

Create a function
Fn(obj)(1,1,1);
output
{
a : {
b : 3,
c : 1
}
d: -1
},

sum(1)(1)(1) => 3

console.log('1')
setTimeout(()=> console.log(2), 1000)
console.log('3')
console.log(new Promise (1));

// 1
// 3
// Pending<>(1)
// 2

// parse
// strigify

{
"name": "Aditya"
}

localStorage.setItem("name", "")
JSON.pase(localStorage.getItem("name"))

JSON.parse() // json in a string


{
"name": "aditya"
}

let arr = [1,2,3,4]

newArr = arr.map(ele => ele*2)

newArr = arr.myMap(ele)

function myMap(data) {
return
}

arr = []

You might also like