# 字符串中的第一个唯一字符

function find(str) {
  let obj = {}
  let i = -1
  const arr = str.split('')

  arr.forEach((item, i) => {
    obj[item] ? obj[item] = 2 : (obj[item] = {i})
  })

  for (let key in obj) {
    console.log(obj[key])
    if (typeof obj[key] === 'object') {
      i = obj[key].i
      break
    }
  }

  return i
}


console.log(find('leetcode'))
console.log(find('loveleetcode'))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Last Updated: 6/27/2023, 7:40:45 PM