আমাদের একটি জাভাস্ক্রিপ্ট ফাংশন লিখতে হবে যা একটি স্ট্রিং নেয় এবং একটি নতুন স্ট্রিং রিটার্ন করে শুধু যেসব শব্দ অরিজিনাল স্ট্রিং-এ একবার বেশি বার আসেছে।
উদাহরণ:
যদি ইনপুট স্ট্রিংটি হয়:
const str = 'this is a is this string that contains that some repeating words';
আউটপুট
তবে আউটপুট হবে:
const output = 'this is that';
এই ফাংশনের জন্য কোড লিখি:
উদাহরণ
এটির জন্য কোড হবে:
const str = 'this is a is this string that contains that some repeating words';
const keepDuplicateWords = str => {
const strArr = str.split(" ");
const res = [];
for(let i = 0; i < strArr.length; i++){
if(strArr.indexOf(strArr[i]) !== strArr.lastIndexOf(strArr[i])){
if(!res.includes(strArr[i])){
res.push(strArr[i]);
};
};
};
return res.join(" ");
};
console.log(keepDuplicateWords(str));
আউটপুট
কনসোলে আউটপুট:
this is that