Skip to content

Commit 21eae96

Browse files
authored
Update js codes.docx
1 parent 203de4b commit 21eae96

1 file changed

Lines changed: 74 additions & 1 deletion

File tree

js codes.docx

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3102,7 +3102,80 @@ However, if you use the assignment operator for a reference value, it will not c
31023102

31033103
JS objects (as non-primitive data types) differ because they have reference values and those values are mutable. This means they share a memory address when shallow copied.
31043104
=========================================================================================================================================================================
3105-
3105+
Airbus Interview Question:
3106+
function countOccurance(arr){
3107+
let map ={}, sortedArr = arr.sort((a,b)=>a-b);
3108+
for(var item of sortedArr){
3109+
if(!(item in map)){ map[item] = 0}
3110+
map[item]++;
3111+
}
3112+
return map;
3113+
}
3114+
console.log(countOccurance([1, 4, 91, 21, 91, 0, 32, 4, 11, 3, 54, 34, 21]))
3115+
-------------------------------------------------------------------
3116+
function countOccurance(arr){
3117+
let map ={}, sortedArr = arr.sort((a,b)=>a-b);
3118+
for(var item of sortedArr){
3119+
if(map[item] ==null){ map[item] = 0}
3120+
map[item]++;
3121+
}
3122+
return map;
3123+
}
3124+
console.log(countOccurance([1, 4, 91, 21, 91, 0, 32, 4, 11, 3, 54, 34, 21]))
3125+
-------------------------------------------------------------------
3126+
function countOccurance(arr){
3127+
let map ={}, sortedArr = arr.sort((a,b)=>a-b);
3128+
for(var item of sortedArr){
3129+
if(map.hasOwnProperty(item)){
3130+
map[item] = map[item] + 1
3131+
}else{
3132+
map[item] = 1;
3133+
}
3134+
}
3135+
return map;
3136+
}
3137+
console.log(countOccurance([1, 4, 91, 21, 91, 0, 32, 4, 11, 3, 54, 34, 21]))
3138+
-------------------------------------------------------------------
3139+
function countOccurance(arr){
3140+
let map ={}, sortedArr = arr.sort((a,b)=>a-b);
3141+
for(var item of sortedArr){
3142+
map[item] = map[item]+1 || 1
3143+
}
3144+
return map
3145+
}
3146+
console.log(countOccurance([1, 4, 91, 21, 91, 0, 32, 4, 11, 3, 54, 34, 21]))
3147+
-------------------------------------------------------------------
3148+
function countOccurance(arr){
3149+
let map ={}, sortedArr = arr.sort((a,b)=>a-b); //[0,1,3,4,4,11,21,21,32,34,54,91,91]
3150+
for(var item of sortedArr){
3151+
map[item] = (map[item] || 0) + 1
3152+
}
3153+
return map
3154+
}
3155+
console.log(countOccurance([1, 4, 91, 21, 91, 0, 32, 4, 11, 3, 54, 34, 21]))
3156+
-------------------------------------------------------------------
3157+
function countOccurance(arr){
3158+
let map ={}, sortedArr = arr.sort((a,b)=>a-b);
3159+
for(var item of sortedArr){
3160+
if(map[item]){
3161+
map[item] = map[item] + 1
3162+
}else{
3163+
map[item] = 1;
3164+
}
3165+
}
3166+
return map;
3167+
}
3168+
console.log(countOccurance([1, 4, 91, 21, 91, 0, 32, 4, 11, 3, 54, 34, 21]))
3169+
-------------------------------------------------------------------
3170+
function countOccurance(arr){
3171+
let map ={}, sortedArr = arr.sort((a,b)=>a-b);
3172+
for (var i=0; i<sortedArr.length; i++)
3173+
{
3174+
map[sortedArr[i]] = map[sortedArr[i]] ? map[sortedArr[i]]+1 : 1;
3175+
}
3176+
return map
3177+
}
3178+
console.log(countOccurance([1, 4, 91, 21, 91, 0, 32, 4, 11, 3, 54, 34, 21]))
31063179
==========================================================================================================================================================================
31073180

31083181

0 commit comments

Comments
 (0)