JSFiddle - React, Tailwind, and code Playground

by ptubig

JavaScript

function hasUniqueChars(s) {
    var map = {};
    
    for (var i = 0; i < s.length; i++) {
        var char = s.charAt(i);

        // Ignore spaces
        if (char == ' ')
            continue;
        
        // If the key already exists in the map, then that
        // character is not unique.
        if (map.hasOwnProperty(char)) {
            return false;
        }
        
        // Here mark the map that this character exists.
        map[char] = true;
    }
    
    return true;
}

alert(hasUniqueChars('peter'));