6.5.9. Counting number of occurrences of an element

6.5.9. এটা উপাদানৰ ঘটনাৰ সংখ্যা গণনা কৰা

In this problem, we are given a series of elements that are stored in an array. Then we are given a single element and we need to find the number of times the given element occurs in the series. Let us understand this with an example.

এই সমস্যাটোত, আমাক এৰেত সঞ্চিত উপাদানৰ এটা শৃংখলা দিয়া হয়। তাৰ পিছত আমাক এটা উপাদান দিয়া হয় আৰু আমি শৃংখলাটোত প্ৰদত্ত উপাদানটো কিমান বাৰ ঘটিব তাৰ সংখ্যা বিচাৰি উলিয়াব লাগিব। আমি ইয়াক এটা উদাহৰণেৰে বুজি পাওঁ আহক।

In the above array, if we are given number 3, then the program should output 2 because the number 3 occurs 2 times in the array.

ওপৰোক্ত এৰেত, যদি আমাক 3 নম্বৰ দিয়া হয়, তেন্তে প্ৰ’গ্ৰামটো আউটপুট 2 হ’ব লাগে কিয়নো 3 নম্বৰটো এৰেত 2 বাৰ হয়।

We can see that the problem is similar to “searching”. So we will be applying a similar logic.

আমি দেখিবলৈ পাওঁ যে সমস্যাটো “অনুসন্ধান”ৰ দৰে। গতিকে আমি একে ধৰণৰ যুক্তি প্ৰয়োগ কৰিম।

We will keep a counter and we will visit all the elements. While visiting an element, we will see whether the element matches with the given element (by performing a comparison operation). If it matches, we will increment the counter.

আমি এটা কাউণ্টাৰ ৰাখিম আৰু আমি সকলো উপাদান দৰ্শন কৰিম। উপাদান এটা দৰ্শন কৰোঁতে, আমি চাম যে উপাদানটো প্ৰদত্ত উপাদানৰ সৈতে মিলা হয় নে নহয় (তুলনা মূলক অপাৰেচন এটা কৰি)। যদি এইটো মিলা হয়, আমি কাউণ্টাৰটো বৃদ্ধি কৰিম।

We will announce the counter value as we finish visiting all the elements in the array.

আমি এৰেৰ সকলো উপাদান দৰ্শন কৰা শেষ কৰাৰ লগে লগে কাউণ্টাৰ মূল্য ঘোষণা কৰিম।

Following code segment does the job.

নিম্নলিখিত কোড খণ্ডটোৱে কামটো কৰে।

Example 6.13 shows the complete C program. Here, we are taking an integer type array with capacity 20. We take the numbers as inputs from the users.

উদাহৰণ 6.13-এ সম্পূৰ্ণ চি প্ৰ’গ্ৰাম টো দেখুৱায়। ইয়াত, আমি 20 ক্ষমতাৰ সৈতে এক ইণ্টেগাৰ প্ৰকাৰৰ এৰে গ্ৰহণ কৰিআছো। আমি সংখ্যাবোৰ ব্যৱহাৰকাৰীৰ পৰা ইনপুট হিচাপে লওঁ।