Ad Code

Responsive Advertisement

Ticker

6/recent/ticker-posts

find the duplicate character in string

 Q.  Write the to code find the duplicate character in the string using javascript

Input: "sonu"

Output: {s:1, o:1,n:1, u:1}


 

function findDuplicate(str) {
    let obj = {}
    for (let i =0; i < str.length; i++) {
        if (obj[s]) {
            obj[s] += 1
        } else {
            obj[s] = 1;
        }
    }
    
    return obj;
}

console.log(findDuplicate("sonu")) 
output: {s:1, o:1,n:1, u:1}

Post a Comment

0 Comments