কম্পিউটার

জাভাস্ক্রিপ্টে পাঠ্যের একটি স্ট্রিংয়ে শীর্ষ তিনটি সবচেয়ে ঘটমান শব্দ খোঁজা


সমস্যা

আমাদের একটি জাভাস্ক্রিপ্ট ফাংশন লিখতে হবে যা একটি ইংরেজি বর্ণমালা স্ট্রিং নেয়। আমাদের ফাংশনটি স্ট্রিং-এ উপস্থিত শীর্ষ তিনটি সর্বাধিক ঘন ঘন শব্দ ফিরিয়ে দেবে৷

উদাহরণ

নিম্নলিখিত কোড -

const str = 'Python was developed by Guido van Rossum in the late eighties and early nineties at the National Research Institute for Mathematics and Computer Science in the Netherlands. Python is derived from many other languages, including ABC, Modula-3, C, C++, Algol-68, SmallTalk, and Unix shell and other scripting languages. Python is copyrighted. Python source code is now available under the GNU General Public License (GPL)';
const findTopThree = (str = '') => {
   str = str
   .replace(/[^\w\s]|_/g, "")
   .replace(/\s+/g, " ")
   .toLowerCase();
   const arr = str.split(' ');
   const map = {};
   arr.forEach(word => {
      map[word] = (map[word] || 0) + 1;
   });
   const res = Array.from(Object.keys(map), key => [key, map[key]]);
   res.sort((a, b) => b[1] - a[1]);
   return [res[0][0], res[1][0], res[2][0]];
};
console.log(findTopThree(str));

আউটপুট

নিম্নোক্ত কনসোল আউটপুট -

["python","the","and"]

  1. জাভাস্ক্রিপ্টে ম্যাট্রিক্সে শব্দ খোঁজা

  2. জাভাস্ক্রিপ্টে একটি স্ট্রিংয়ে হ্যামিং দূরত্ব খোঁজা

  3. জাভাস্ক্রিপ্টে স্ট্রিংয়ে ন্যূনতম মুছে ফেলার সন্ধান করা

  4. জাভাস্ক্রিপ্টে একটি বাক্য থেকে n সবচেয়ে ঘন ঘন শব্দ খোঁজা