-
-
Save msaxena25/62d17a1c9b1e507f2eafd83fc7d54db0 to your computer and use it in GitHub Desktop.
function duplicateCharCount(str) { | |
if(str) { | |
var obj = {}; | |
for(let i = 0; i < str.length; i++) { | |
if(obj[str[i]]){ | |
obj[str[i]] += obj[str[i]]; | |
}else { | |
obj[str[i]] = 1; | |
} | |
} | |
console.log(obj); | |
} | |
} | |
Run this Code > | |
duplicateCharCount("aabcdd"); | |
Output > | |
{a: 2, b: 1, c: 1, d: 2} |
@lukebergmann Thank you so much for the response, I really appreciate it! I just have one more question if you don't mind. How would I go about trying to remove the occurrences of letters with only 1 value, in essence only including repeated characters in the output. I was thinking of somehow targeting the certain letters with a value of 1 and removing them in some way possibly via a method. I just haven't learned objects yet and I don't know how to target those certain values and remove them.
Thanks again!
Stanley
@Stan-l-e-y Not a worry! You are definitely on the right track with the method idea. Understanding objects can be a bit tricky if you have not really learnt them yet. If this was an array you could totally use the filter() method that would allow you to filter out the values that are not needed in the returned result. Unfortunately, this is not an array and does require a few extra steps. I won't go too far into it now, but once you start learning objects feel free to shoot me a message! MDN docs and StackOverflow are a great way to learn. For now, I'll leave a couple of hints here and you can give it a try yourself!
- Object.keys() - Returns an array.
- Array.prototype.filter() - Creates a new array with all elements that pass the test implemented by the provided function.
- Array.prototype.includes() - Determines whether an array includes a certain value among its entries.
- Array.prototype.reduce() - Can help build the new Object with only the specific key-values you want inside.
Referenced from this StackOverflow Article
Have fun!
Luke
@lukebergmann Thanks once again Luke! I'll take a look at these and explore my possibilities. Cheers!
let str= 'CormSquare';
let obj= {}
for(val of str){
obj[val] = (obj[val] || 0) + 1
}
console.log(obj)
Output: { C: 1, o: 1, r: 2, m: 1, S: 1, q: 1, u: 1, a: 1, e: 1 }
@Stan-l-e-y There are two main things to consider for this example, Knowing how loops work, and knowing how to access and add values to objects. For this specific example, the 'if' is checking if a letter has been added as a key in the object named obj. If a letter is a key in the object, or the 'if' statement is true, and it finds a matching letter in the string, it adds a value of 1 to the total. If a letter is not yet a key in the object, the 'if' statement is false, triggering the 'else' statement. The 'else' statement creates the key-value pair in the object and the loop continues.
If the string was "Canada" it would look something like this:
Hope this helps with this example, let me know if you have any other questions!
Luke