Home > Backend Development > C++ > Detailed explanation of the mode function in C++

Detailed explanation of the mode function in C++

WBOY
Release: 2023-11-18 15:08:23
Original
1707 people have browsed it

Detailed explanation of the mode function in C++

Detailed explanation of the mode function in C

In statistics, the mode refers to the value that appears most frequently in a set of data. In C language, we can find the mode in any set of data by writing a mode function.

The mode function can be implemented in many different ways. Two of the commonly used methods will be introduced in detail below.

The first method is to use a hash table to count the number of times each number appears. First, we need to define a hash table with each number as the key and the number of occurrences as the value. Then, for the given data set, we iterate through each number, comparing it to the key in the hash table. If the number already exists in the hash table, we add 1 to the corresponding value; if the number does not exist in the hash table, we add it to the hash table and set its value to 1. Finally, we traverse the hash table and find the number that appears the most often, which is the mode.

The following is a code example that uses a hash table to implement the majority function:

#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;

int majorityElement(vector<int>& nums) {
    unordered_map<int, int> count;
    for (int num : nums) {
        count[num]++;
    }
    int majority = nums[0];
    int maxCount = count[nums[0]];
    for (auto& pair : count) {
        if (pair.second > maxCount) {
            majority = pair.first;
            maxCount = pair.second;
        }
    }
    return majority;
}

int main() {
    vector<int> nums = {1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4};
    int result = majorityElement(nums);
    cout << "The majority element is: " << result << endl;
    return 0;
}
Copy after login

Execute the above code, the output result is:
The majority element is: 4

Another approach is to exploit the properties of arrays for optimization. First, we sort the dataset. Then, we iterate through the sorted array and count the number of times each number appears. Since the same number will appear continuously, you can determine whether it appears continuously by comparing whether the current number and the previous number are equal. If the current number is equal to the previous number, the count is incremented by 1; otherwise, we start counting again. Finally, we find the number with the largest count, which is the mode.

The following is a code example of the majority function optimized by array sorting:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int majorityElement(vector<int>& nums) {
    sort(nums.begin(), nums.end());
    int majority = nums[0];
    int count = 1;
    int maxCount = 1;
    for (int i = 1; i < nums.size(); i++) {
        if (nums[i] == nums[i - 1]) {
            count++;
        } else {
            count = 1;
        }
        if (count > maxCount) {
            majority = nums[i];
            maxCount = count;
        }
    }
    return majority;
}

int main() {
    vector<int> nums = {1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4};
    int result = majorityElement(nums);
    cout << "The majority element is: " << result << endl;
    return 0;
}
Copy after login

Execute the above code, the output result is:
The majority element is: 4

The above are two commonly used methods to implement the mode function in C. These methods are highly efficient and can correctly find the mode on various data sets. Based on actual needs and data characteristics, we can choose to use one of these methods to solve specific problems. I hope this article will help you understand the mode function in C.

The above is the detailed content of Detailed explanation of the mode function in C++. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template