Conversation
Another solution without additional data structures but using objects, with performance O(N).
function isUnique3(str){
let obj = {}
for( let elem of str){
if(obj.hasOwnProperty(elem)) obj[elem]++
else obj[elem]=1
}
for( key in obj){
if (obj[key]=== 1) return true
else return false
}
}
isUnique3('ada') // false
isUnique3("golden") // true
lhchavez
left a comment
There was a problem hiding this comment.
Hi,
welcome!
This is a solution of this exercise using JS obejcts.
P.S. Am I supposed to run the mocha tests in my machine?
yes. that way you can quickly see if the algorithm as implemented solves the desired problem. in this case, the tests seem to not be passing, so it is likely that the solution is not correct :(
rather than returning an array, the function should return true if all characters in the string are unique or false otherwise (the name is a hint: isUnique is something that hints at a yes/no answer).
THanks!
| assert.equal(isUnique1("tech"), true); | ||
| assert.equal(isUnique2("tech"), true); |
There was a problem hiding this comment.
| assert.equal(isUnique1("tech"), true); | |
| assert.equal(isUnique2("tech"), true); | |
| assert.equal(isUnique("tech"), true); |
there's only one implementation in this file. same below.
| @@ -1,3 +1,4 @@ | |||
|
|
|||
There was a problem hiding this comment.
this seems like an unintended change in another person's file:
Hi,
This is a solution of this exercise using JS obejcts.
P.S. Am I supposed to run the mocha tests in my machine?
THanks!