find the first duplicate character in a string
2022年5月1日小于 1 分钟
find the first duplicate character in a string
Question
Given a string which might have duplicate letters, write a function to find the first duplicate.
firstDuplicate('abca')
// 'a'
firstDuplicate('abcdefe')
// 'e'
firstDuplicate('abcdef')
// null
What is the time & space cost of your approach ? Can you do better?
Code
/**
* @param {string} str
* @return {string | null}
*/
function firstDuplicate(str) {
// your code here
}