हमें एक जावास्क्रिप्ट फंक्शन लिखना है जो डुप्लिकेट एंट्रीज वाले नंबर्स का ऐरे ले और सभी डुप्लिकेट एंट्रीज को एक इंडेक्स पर जोड़ दे।
उदाहरण
इसके लिए कोड:
const input = [1, 3, 1, 3, 5, 7, 5, 3, 4]; const sumDuplicate = arr => { const map = arr.reduce((acc, val) => { if(acc.has(val)){ acc.set(val, acc.get(val) + 1); }else{ acc.set(val, 1); }; return acc; }, new Map()); return Array.from(map, el => el[0] * el[1]); }; console.log(sumDuplicate(input));आउटपुट
कंसोल में आउटपुट:
[ 2, 9, 10, 7, 4 ]