The name count
is too vague for such a specific task. I suggest countReappearingChars
instead.
Avoid big blocks of variable definitions. Code is easier to read and maintain when variables are declared and initialized near where they are used. For example, with a block like this, it's harder to notice that result
is never used.
nArr
does not need to be an array. You can just keep a count.
obj
is cryptically named. I suggest calling it charCount
.
var arr = str.split("")
is unnecessary; you can iterate over str
directly.
"occurances" is misspelled in the comment.
This line is hard to read:
obj[letter] === undefined ? obj[letter] = 1 : obj[letter] += 1;
It would be more clearly be written using if-else, and therefore should be:
if (obj[letter] === undefined) {
obj[letter] = 1;
} else {
obj[letter] += 1;
}
I would consider your ternary operator to be abusive, since you are not using it as as an expression. In contrast, this expression could be considered appropriate, since the right-hand side of the assignment is an expression:
obj[letter] = (obj[letter] === undefined) ? 1 : obj[letter] + 1;
But that still looks cumbersome. If you want to get clever with it, you could use || 0
, as shown below.
function countReappearingChars(str) {
// Count all occurrences in str
var charCount = {};
for (var i = 0; i < str.length; i++) {
charCount[str[i]] = 1 + (charCount[str[i]] || 0);
}
// Loop over and filter charCount
var result = 0;
for (var char in charCount) {
if (charCount[char] > 1) result++;
}
return result;
}