Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // function countCharacter(inputStr) {
- // convert the string to lowercase
- // const lowerCaseStr = inputStr.toLowerCase()
- // Remove Spaces and special characters
- // const cleanStr = lowerCaseStr.replace(/[^a-z0-9]/gi, '')
- // create a object to hold the counts or frequency
- // const charCount = {}
- // loop the cleaned string and count each character
- // for (let i = 0; i < cleanStr.length; i++) {
- // const char = cleanStr[i]
- // if (charCount[char]) {
- // charCount[char] += 1
- // } else {
- // charCount[char] = 1
- // }
- // }
- // return the result
- // return charCount
- // }
- // countCharacter('Hel @lo') // Output: { h: 1, e: 1, l: 2, o: 1 }
- //optimization
- //Time complexity - O(n + n) = O(n)
- //Space complexity- O(n)
- // function countCharacter(inputStr) {
- // convert the string to lowercase
- // const lowerCaseStr = inputStr.toLowerCase()
- // create a object to hold the counts or frequency
- // const charCount = {}
- // loop the cleaned string and count each character
- // for (let i = 0; i < lowerCaseStr.length; i++) {
- // const char = lowerCaseStr[i]
- // Remove Spaces and special characters during add frequency of the character to object
- // if ((char >= 'a' && char <= 'z') || (char >= '0' && char <= '9')) {
- //ternary operator
- // charCount[char] = charCount[char] ? charCount[char] + 1 : 1
- //Alternate solution
- // charCount[char] = (charCount[char] || 0) + 1
- // }
- // }
- // return the result
- // return charCount
- // }
- // console.log(countCharacter('Hel @lo'))
- // console.log(countCharacter('')) // Output: {}
- // console.log(countCharacter('Hello123!@#Hello'))
- // console.log(countCharacter('héllo😊héllo'))
- //time complexity O(n * m) -> O(nm)
- //space complexity - O(1)
- // function isElementExists(arr1, arr2) {
- // loop arr1 and check each element with 2nd array element
- // for (let elm of arr1) {
- //compare each element with 2nd arr element
- // if (arr2.includes(elm)) {
- // return true
- // }
- // for (let elm2 of arr2) {
- // if (elm === elm2) {
- // return true
- // }
- // }
- // }
- // return false
- // }
- //Time Complexity O(m + n)
- //Space complexity - O(n)
- function isElementExists(arr1, arr2) {
- //loop arr1 and tack down each data
- // const trackingObj = {}
- // for (let elm of arr1) {
- // trackingObj[elm] = true
- // }
- const arr1Set = new Set(arr1)
- console.log(arr1Set)
- //loop arr2 and check each element existence in the tracking Object
- for (let elm of arr2) {
- // if (elm in trackingObj) {
- // return true
- // }
- if (arr1Set.has(elm)) {
- return true
- }
- }
- return false
- }
- console.log(isElementExists(['a', 'b', 'z'], [1, 2, 3, 'z']))
- console.log(isElementExists(['a'], [])) // Output: false
- console.log(isElementExists([], [1, 2, 3])) // Output: false
- console.log(isElementExists(['x', 'y', 'z'], [1, 2, 3, 'a'])) // Output: false
- console.log(isElementExists(['a', 'b', 'c'], [1, 2, 3, 'b'])) // Output: true
- console.log(isElementExists(['a', 'b', 'c'], ['a', 'b', 'c'])) // Output: true
- console.log(isElementExists(['a', 'b', 'c'], ['a', 'b', 'c'])) // Output: true
- console.log(isElementExists(['1', '2', '3'], [1, 2, 3])) // Output: false
- console.log(isElementExists([1, 2, 3], [1, 2, 3, '1'])) // Output: true
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement