<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 數組中最頻繁的元素 > 原文: [https://www.geeksforgeeks.org/frequent-element-array/](https://www.geeksforgeeks.org/frequent-element-array/) 給定一個數組,找到其中最頻繁的元素。 如果有多個元素出現的次數最多,請打印其中的任何一個。 **示例**: ``` Input : arr[] = {1, 3, 2, 1, 4, 1} Output : 1 1 appears three times in array which is maximum frequency. Input : arr[] = {10, 20, 10, 20, 30, 20, 20} Output : 20 ``` 一個簡單的**解決方案**是運行兩個循環。 外循環一一挑選所有元素。 內循環找到所拾取元素的頻率,并與迄今為止的最大值進行比較。 該解決方案的時間復雜度為 `O(n^2)` 更好的**解決方案**是進行排序。 我們首先對數組進行排序,然后線性遍歷數組。 ## C++ ```cpp // CPP program to find the most frequent element // in an array. #include <bits/stdc++.h> using namespace std; int mostFrequent(int arr[], int n) { ????// Sort the array ????sort(arr, arr + n); ????// find the max frequency using linear traversal ????int max_count = 1, res = arr[0], curr_count = 1; ????for (int i = 1; i < n; i++) { ????????if (arr[i] == arr[i - 1]) ????????????curr_count++; ????????else { ????????????if (curr_count > max_count) { ????????????????max_count = curr_count; ????????????????res = arr[i - 1]; ????????????} ????????????curr_count = 1; ????????} ????} ????// If last element is most frequent ????if (curr_count > max_count) ????{ ????????max_count = curr_count; ????????res = arr[n - 1]; ????} ????return res; } // driver program int main() { ????int arr[] = { 1, 5, 2, 1, 3, 2, 1 }; ????int n = sizeof(arr) / sizeof(arr[0]); ????cout << mostFrequent(arr, n); ????return 0; } ``` ## Java ```java //Java program to find the most frequent element //in an array import java.util.*; class GFG { ????static int mostFrequent(int arr[], int n) ????{ ????????// Sort the array ????????Arrays.sort(arr); ????????// find the max frequency using linear ????????// traversal ????????int max_count = 1, res = arr[0]; ????????int curr_count = 1; ????????for (int i = 1; i < n; i++) ????????{ ????????????if (arr[i] == arr[i - 1]) ????????????????curr_count++; ????????????else? ????????????{ ????????????????if (curr_count > max_count) ????????????????{ ????????????????????max_count = curr_count; ????????????????????res = arr[i - 1]; ????????????????} ????????????????curr_count = 1; ????????????} ????????} ????????// If last element is most frequent ????????if (curr_count > max_count) ????????{ ????????????max_count = curr_count; ????????????res = arr[n - 1]; ????????} ????????return res; ????} ????// Driver program ????public static void main (String[] args) { ????????int arr[] = {1, 5, 2, 1, 3, 2, 1}; ????????int n = arr.length; ????????System.out.println(mostFrequent(arr,n)); ????} } // This code is contributed by Akash Singh. ``` ## Python3 ```py # Python3 program to find the most? # frequent element in an array. def mostFrequent(arr, n): ????# Sort the array ????arr.sort() ????# find the max frequency using ????# linear traversal ????max_count = 1; res = arr[0]; curr_count = 1 ????for i in range(1, n):? ????????if (arr[i] == arr[i - 1]): ????????????curr_count += 1 ????????else : ????????????if (curr_count > max_count):? ????????????????max_count = curr_count ????????????????res = arr[i - 1] ????????????curr_count = 1 ????# If last element is most frequent ????if (curr_count > max_count): ????????max_count = curr_count ????????res = arr[n - 1] ????return res # Driver Code arr = [1, 5, 2, 1, 3, 2, 1]? n = len(arr) print(mostFrequent(arr, n)) # This code is contributed by Smitha Dinesh Semwal. ``` ## C# ```cs // C# program to find the most? // frequent element in an array using System; class GFG { ????static int mostFrequent(int []arr, int n) ????{ ????????// Sort the array ????????Array.Sort(arr); ????????// find the max frequency using? ????????// linear traversal ????????int max_count = 1, res = arr[0]; ????????int curr_count = 1; ????????for (int i = 1; i < n; i++) ????????{ ????????????if (arr[i] == arr[i - 1]) ????????????????curr_count++; ????????????else ????????????{ ????????????????if (curr_count > max_count) ????????????????{ ????????????????????max_count = curr_count; ????????????????????res = arr[i - 1]; ????????????????} ????????????????curr_count = 1; ????????????} ????????} ????????// If last element is most frequent ????????if (curr_count > max_count) ????????{ ????????????max_count = curr_count; ????????????res = arr[n - 1]; ????????} ????????return res; ????} ????// Driver code ????public static void Main ()? ????{ ????????int []arr = {1, 5, 2, 1, 3, 2, 1}; ????????int n = arr.Length; ????????Console.WriteLine(mostFrequent(arr,n)); ????} } // This code is contributed by vt_m. ``` ## PHP ```php <?php // PHP program to find the // most frequent element // in an array. function mostFrequent( $arr, $n) { ????// Sort the array ????sort($arr); ????sort($arr , $n); ????// find the max frequency? ????// using linear traversal ????$max_count = 1;? ????$res = $arr[0];? ????$curr_count = 1; ????for ($i = 1; $i < $n; $i++)? ????{ ????????if ($arr[$i] == $arr[$i - 1]) ????????????$curr_count++; ????????else? ????????{ ????????????if ($curr_count > $max_count) ????????????{ ????????????????$max_count = $curr_count; ????????????????$res = $arr[$i - 1]; ????????????} ????????????$curr_count = 1; ????????} ????} ????// If last element? ????// is most frequent ????if ($curr_count > $max_count) ????{ ????????$max_count = $curr_count; ????????$res = $arr[$n - 1]; ????} ????return $res; } // Driver Code { ????$arr = array(1, 5, 2, 1, 3, 2, 1); ????$n = sizeof($arr) / sizeof($arr[0]); ????echo mostFrequent($arr, $n); ????return 0; } // This code is contributed by nitin mittal ?> ``` **輸出**: ``` 1 ``` 時間復雜度:`O(N log N)` 輔助空間:`O(1)` **有效解決方案**是使用哈希。 我們創建一個哈希表,并將元素及其頻率計數存儲為鍵值對。 最后,我們遍歷哈希表并打印具有最大值的鍵。 ## C++ ``` // CPP program to find the most frequent element // in an array. #include <bits/stdc++.h> using namespace std; int mostFrequent(int arr[], int n) { ????// Insert all elements in hash. ????unordered_map<int, int> hash; ????for (int i = 0; i < n; i++) ????????hash[arr[i]]++; ????// find the max frequency ????int max_count = 0, res = -1; ????for (auto i : hash) { ????????if (max_count < i.second) { ????????????res = i.first; ????????????max_count = i.second; ????????} ????} ????return res; } // driver program int main() { ????int arr[] = { 1, 5, 2, 1, 3, 2, 1 }; ????int n = sizeof(arr) / sizeof(arr[0]); ????cout << mostFrequent(arr, n); ????return 0; } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看