Created
May 6, 2020 06:02
-
-
Save heyitsarpit/ae9784edf6fb8a6dfc2565278962337a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// write a funcction which will find the number of time | |
// a character is repeated in a string | |
// input: aabbcccddefb | |
// output: { a: 2, b: 3,c: 3, d: 2, e: 1,f:1} | |
export function charOccurance(string: string) { | |
let freq: Record<string, number> = {}; | |
const strArr = Array.from(string); | |
strArr.forEach(char => { | |
if (freq[char]) { | |
freq[char] += 1; | |
} else { | |
freq[char] = 1; | |
} | |
}); | |
return freq; | |
} | |
// Write a function to flat a array | |
// input: [[1],2,3,[4,5,[6,7]]] | |
// output: [1,2,3,4,5,6,7] | |
export function flatArray(arr: any[]) { | |
let flatArr: any[] = []; | |
arr.forEach(item => { | |
if (Array.isArray(item)) { | |
flatArr = flatArr.concat(flatArray(item)); | |
} else { | |
flatArr.push(item); | |
} | |
return flatArr; | |
}); | |
return flatArr; | |
} | |
console.log(charOccurance("aabbcccddefb")); | |
console.log(flatArray([[1], 2, 3,[[[[[[6]]]]]], [4, 5, [6, 7]]])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment