Snippets Collections
var number = "73-84-4"
cardNumber1 = number.filter { $0 != "-" }
print("card 122", number)
// SOLUTION 1 - MY (DISCOVERED) SOLUTION (RUNTIME - 84MS, MEMORY - 41.2MB)
var firstUniqChar = function(s) {
  // loop through the characters of the string
  for (var i = 0; i < s.length; i++) {
    // set a variable for the char
    var c = s.charAt(i);
    // if the index of the char == i, and the index of 'c', starting search from index 'i + 1' == -1
    if (s.indexOf(c) == i && s.indexOf(c, i + 1) == -1) {
      return s.indexOf(c);
    }
  }
  return -1;
};

// SOLUTION 2 (BEST RUNTIME - 68MS)
var firstUniqChar = function(s) {
  count = []

    for(let i=0;i<s.length;i++){
      index = s.charCodeAt(i)-'a'.charCodeAt(0)
   
      if(count[index]==undefined){
        count[index]=1
      }else{
        count[index]++
      }
    }
    
    for(let i=0;i<s.length;i++){
      index = s.charCodeAt(i)-'a'.charCodeAt(0)
      if(count[index]==1){
        return i
      }
    }
    return -1
};

// SOLUTION 3 - (RUNTIME - 96MS)
var firstUniqChar = function(s) {
    for (i=0; i < s.length; i++) {
      if (s.indexOf(s[i]) == s.lastIndexOf(s[i])) {
        return i
      }
    }
   return -1
}
star

Fri Apr 01 2022 12:12:44 GMT+0000 (Coordinated Universal Time)

#ios #swift #remove #removecharacters #char #characters #string
star

Fri Sep 17 2021 16:21:34 GMT+0000 (Coordinated Universal Time) https://leetcode.com/submissions/detail/555922232/?from=explore&item_id=881

#javascript #strings #uniquecharacter #characters #loops #unsolved

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension