Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const a = [
- [1, 0, 1],
- [0, 1, 0],
- [1, 1, 1],
- ];
- console.log(a.flat().filter(v=>v===1).length);
- let count=0
- for(let i=0;i<a.length;i++){
- for(let j=0;j<a[i].length;j++){
- if(a[i][j]===1) count++
- }
- }
- console.log(count);
- const b = [8, 7, [7, [7], 9]];
- function flattenArray(a, result = []) {
- for (let val of a) {
- if (!Array.isArray(val)) {
- result.push(val);
- } else {
- flattenArray(val, result);
- }
- }
- return result
- }
- console.log(flattenArray(b));
- class BinarySearchTree{
- constructor(params) {
- this.root= null
- this.sum=0
- }
- preOrder(curr= this.root){
- if(curr){
- this.sum+= curr.val
- this.preOrder(curr.left)
- this.preOrder(curr.right)
- }
- }
- }
- const str= "hghyyydddddddd"
- function longestRepeating(str){
- let strArr= str.split(''), f={}
- for(let val of strArr){
- if(f[val]) f[val]++
- else f[val]=1
- }
- let mostRepeated = Object.entries(f).reduce( (acc,curr)=>{
- return acc=curr[1]>acc[1]?curr:acc
- }, Object.entries(f)[0] )[0]
- const ans = strArr.slice( strArr.indexOf(mostRepeated), strArr.lastIndexOf(mostRepeated) )
- return ans.join('')
- }
- console.log(longestRepeating(str));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement