Last active
February 13, 2019 15:14
-
-
Save MahmoudAgamy/d726df66a0bd9e48fea269545607c29c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let string = "aabbbcbck" | |
let strArr = string.split("") // [ 'a', 'a', 'b', 'b', 'b', 'c', 'b', 'c', 'k' ] | |
// Array to object using For-Loop - counting each letter occurances | |
let usingForLoop = (arr)=>{ | |
let newObj={}; | |
for(let i=0; i<arr.length; ++i){ | |
newObj[arr[i]] = newObj[arr[i]]+1 || 1; | |
} | |
return newObj; | |
} | |
console.log("using For Loop: ", usingForLoop(strArr)) //{ a: 2, b: 4, c: 2, k: 1 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment