समस्या
हमें एक जावास्क्रिप्ट फ़ंक्शन लिखना है जो एक स्ट्रिंग लेता है। हमारा फ़ंक्शन ठीक दो तत्वों की एक सरणी लौटाएगा: पहला तत्व वह कैरेक्टर होगा जो स्ट्रिंग में सबसे अधिक बार लगातार आता है और दूसरा उसकी उपस्थिति की संख्या होगी।
उदाहरण
निम्न कोड देखें:
const str = 'tdfdffddffsdsfffffsdsdsddddd'; const findConsecutiveCount = (str = '') => { let res=''; let count=1; let arr = [] for (let i=0;i<str.length;i++){ if (str[i]===str[i+1]){ count++ } else { if (arr.every(v=>v<count)){ res=str[i]+count } arr.push(count) count=1 } } return !res?['',0]:[res.slice(0,1),res.slice(1)*1]; }; console.log(findConsecutiveCount(str));आउटपुट
['f', 5]