Advertisement
ListonFermi

Week 14

Mar 20th, 2024
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 1.30 KB | Source Code | 0 0
  1. const a = [
  2.   [1, 0, 1],
  3.   [0, 1, 0],
  4.   [1, 1, 1],
  5. ];
  6.  
  7. console.log(a.flat().filter(v=>v===1).length);
  8.  
  9. let count=0
  10. for(let i=0;i<a.length;i++){
  11.     for(let j=0;j<a[i].length;j++){
  12.         if(a[i][j]===1) count++
  13.     }
  14. }
  15.  
  16. console.log(count);
  17.  
  18. const b = [8, 7, [7, [7], 9]];
  19.  
  20. function flattenArray(a, result = []) {
  21.   for (let val of a) {
  22.     if (!Array.isArray(val)) {
  23.       result.push(val);
  24.     } else {
  25.       flattenArray(val, result);
  26.     }
  27.   }
  28.   return result
  29. }
  30.  
  31. console.log(flattenArray(b));
  32.  
  33.  
  34.  
  35. class BinarySearchTree{
  36.  
  37.     constructor(params) {
  38.         this.root= null
  39.         this.sum=0
  40.     }
  41.  
  42.     preOrder(curr= this.root){
  43.         if(curr){
  44.             this.sum+= curr.val
  45.             this.preOrder(curr.left)
  46.             this.preOrder(curr.right)
  47.         }
  48.     }  
  49. }
  50.  
  51. const str= "hghyyydddddddd"
  52.  
  53. function longestRepeating(str){
  54.  
  55.     let strArr= str.split(''), f={}
  56.  
  57.     for(let val of strArr){
  58.         if(f[val]) f[val]++
  59.         else f[val]=1
  60.     }
  61.  
  62.     let mostRepeated = Object.entries(f).reduce( (acc,curr)=>{
  63.         return acc=curr[1]>acc[1]?curr:acc
  64.     }, Object.entries(f)[0] )[0]
  65.  
  66.     const ans = strArr.slice( strArr.indexOf(mostRepeated), strArr.lastIndexOf(mostRepeated) )
  67.  
  68.     return ans.join('')
  69. }
  70.  
  71. console.log(longestRepeating(str));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement