Is Unique (1.1 Cracking the Coding Interview)
Jul 26, 2021
This algorithm is to determine if a string has all unique characters. The idea is to make a hash as a frequency counter that takes into account how times a letter occurs.
Loop through the hash using the Object Keys using the map function. Next, use logic that states, “if the value is equal to 1, set a variable equal to true and return it, else set a variable equal to false and return it.
Here is my solution!
const inUnique = (string) => {
let ds = {};
let result = string.split(""); for (let ele of result) {
ds[ele] = (ds[ele] || 0) + 1; } let ans; Object.values(ds).forEach((x) => {
if (x === 1) {
ans = true;
} else {
ans = false;
}
}); return ans; }; inUnique("gilbert");
Happy Hacking!
-Crystal