Hướng dẫn maximum repeating count in python - số lần lặp lại tối đa trong python

Đưa ra một loạt các số nguyên có độ dài n, chương trình phải tìm giá trị lặp lại với số lần tối đa và in số. Trong trường hợp cà vạt, chọn số nhỏ hơn và in nó.

Điều kiện biên: Độ dài của mảng N sẽ từ 2 đến 100
Length of array N will be from 2 to 100

Định dạng đầu vào: Dòng đầu tiên sẽ chứa mảng các số nguyên có độ dài n được phân tách bằng một hoặc nhiều khoảng trắng.
First line will contain the array of integers of length N separated by one or more spaces.

Định dạng đầu ra: Giá trị số nguyên lặp lại số lần tối đa.
The integer value which repeats the maximum number of times.

Mẫu đầu vào/đầu ra: Ví dụ 1: Đầu vào: 10 20 30 20 30 10 30 20 Nhưng 20 là số nhỏ hơn và do đó 20 được in dưới dạng đầu ra. Ví dụ 2:
Example 1:
Input:
10 20 30 20 30 10 30 20
Output:
20
Explanation:
Both 20 and 30 repeats three times. But 20 is the smaller number and hence 20 is printed as output. Example 2:

Đầu vào: 1 2 3 5 9 2 9 6 9Output: 9 Explanation: 9 Lặp lại ba lần nhiều hơn số lượng lặp lại của bất kỳ số nào khác.
1 2 3 5 9 2 9 6 9
Output:
9
Explanation:
9 repeats thrice which is more than the repetition count of any other number.

n=[int(i) for i in input().split()]
print(max(n,key=n.count))

The maximum repeating number is 3
5namespace88int7
The maximum repeating number is 3
71

Đầu ranaive approach is to run two loops, the outer loop picks an element one by one, and the inner loop counts a number of occurrences of the picked element. Finally, return the element with a maximum count. The time complexity of this approach is O(n^2).
A better approach is to create a count array of size k and initialize all elements of count[] as 0. Iterate through all elements of input array, and for every element arr[i], increment count[arr[i]]. Finally, iterate through count[] and return the index with maximum value. This approach takes O(n) time, but requires O(k) space.

Sau đây là thời gian O (n) và O (1) Phương pháp tiếp cận không gian bổ sung. & Nbsp; Hãy cho chúng tôi hiểu cách tiếp cận với một ví dụ đơn giản trong đó ARR [] = {2, 3, 3, 5, 3, 4, 1, 7} , k = 8, n = 8 (số phần tử trong mảng []).O(n) time and O(1) extra space approach. 
Let us understand the approach with a simple example where arr[] = {2, 3, 3, 5, 3, 4, 1, 7}, k = 8, n = 8 (number of elements in arr[]).

  1. Lặp mặc dù mảng đầu vào ARR [], cho mọi phần tử ARR [i], tăng arr [mảng [i]%k] bởi k (mảng [] trở thành {2, 11, 11, 29, 11, 12, 1, 15} )
  2. Tìm giá trị tối đa trong mảng đã sửa đổi (giá trị tối đa là 29). Chỉ số của giá trị tối đa là phần tử lặp lại tối đa (chỉ mục 29 là 3).
  3. Nếu chúng tôi muốn lấy lại mảng gốc, chúng tôi có thể lặp lại qua mảng một lần nữa và làm ARR [i] = mảng [i] % k trong đó tôi thay đổi từ 0 đến N-1.

Thuật toán trên hoạt động như thế nào? Vì chúng tôi sử dụng ARR [i]%K làm chỉ mục và thêm giá trị k tại chỉ số ARR [i]%k, chỉ số bằng phần tử lặp lại tối đa sẽ có giá trị tối đa cuối cùng. Lưu ý rằng K được thêm số lần tối đa tại chỉ số bằng phần tử lặp lại tối đa và tất cả các phần tử mảng nhỏ hơn K.following là triển khai C ++ của thuật toán trên. & NBSP;
Following is C++ implementation of the above algorithm. 

C++

#include

using namespace std;

int maxRepeating(int* arr, int

The maximum repeating number is 3
1int
The maximum repeating number is 3
3

The maximum repeating number is 3
4

The maximum repeating number is 3
5
The maximum repeating number is 3
6
The maximum repeating number is 3
7int
The maximum repeating number is 3
9

#include0#include1

The maximum repeating number is 3
5int #include4

The maximum repeating number is 3
5
The maximum repeating number is 3
6
The maximum repeating number is 3
7int #include9

The maximum repeating number is 3
5
The maximum repeating number is 3
4

#include0using3 using4

#include0

The maximum repeating number is 3
4

using7using8

using7namespace0

#include0namespace2

The maximum repeating number is 3
5namespace2

The maximum repeating number is 3
5namespace6 namespace7

namespace2

int std;0

The maximum repeating number is 3
4

The maximum repeating number is 3
5int std;4

The maximum repeating number is 3
5int std;7std;8std;9std;8int1

The maximum repeating number is 3
5int int4

The maximum repeating number is 3
5int6int7 int8

int9maxRepeating(0

The maximum repeating number is 3
5namespace6 maxRepeating(3

namespace2

Java

maxRepeating(5 maxRepeating(6

maxRepeating(7 maxRepeating(8

The maximum repeating number is 3
5int0 int maxRepeating(int int4int
The maximum repeating number is 3
1int
The maximum repeating number is 3
3

The maximum repeating number is 3
5
The maximum repeating number is 3
4

#include0

The maximum repeating number is 3
6
The maximum repeating number is 3
7int * arr, 5* arr, 6* arr, 7

using7* arr, 9

#include0____6

The maximum repeating number is 3
022__96 ____1044 ____96
The maximum repeating number is 3
06

#include0

The maximum repeating number is 3
6
The maximum repeating number is 3
7int * arr, 5
The maximum repeating number is 3
12
The maximum repeating number is 3
13

#include0

The maximum repeating number is 3
4

using7using3 using4

using7

The maximum repeating number is 3
4

The maximum repeating number is 3
21using8

The maximum repeating number is 3
21namespace0

using7namespace2

#include0namespace2

#include0namespace6 namespace7

The maximum repeating number is 3
5namespace2

The maximum repeating number is 3
5
The maximum repeating number is 3
35 int0
The maximum repeating number is 3
37
The maximum repeating number is 3
38

The maximum repeating number is 3
5
The maximum repeating number is 3
4

#include0

The maximum repeating number is 3
6
The maximum repeating number is 3
7int * arr, 5* arr, 6* arr, 7

#include0____6

The maximum repeating number is 3
022__96 ____1044 ____96
The maximum repeating number is 3
06

#include0

The maximum repeating number is 3
6
The maximum repeating number is 3
7int * arr, 5
The maximum repeating number is 3
12
The maximum repeating number is 3
13

using7using3 using4

The maximum repeating number is 3
72
The maximum repeating number is 3
73

The maximum repeating number is 3
5namespace2

namespace2

Python3

#include0namespace6 namespace7

The maximum repeating number is 3
5
The maximum repeating number is 3
35 int0
The maximum repeating number is 3
37
The maximum repeating number is 3
38

A

#include0____6

The maximum repeating number is 3
62

#include0____6

The maximum repeating number is 3
65
The maximum repeating number is 3
66
The maximum repeating number is 3
06

#include0

The maximum repeating number is 3
69
The maximum repeating number is 3
70
The maximum repeating number is 3
71

The maximum repeating number is 3
77
The maximum repeating number is 3
78

The maximum repeating number is 3
5
The maximum repeating number is 3
6
The maximum repeating number is 3
81
The maximum repeating number is 3
82
The maximum repeating number is 3
83
The maximum repeating number is 3
7int____
The maximum repeating number is 3
86

#include0

The maximum repeating number is 3
88
The maximum repeating number is 3
89____190
The maximum repeating number is 3
71
The maximum repeating number is 3
92
The maximum repeating number is 3
93

The maximum repeating number is 3
5
The maximum repeating number is 3
95
The maximum repeating number is 3
92
The maximum repeating number is 3
97* arr, 6
The maximum repeating number is 3
99

The maximum repeating number is 3
5#include019192 * arr, 6

The maximum repeating number is 3
5
The maximum repeating number is 3
6
The maximum repeating number is 3
81
The maximum repeating number is 3
82
The maximum repeating number is 3
83
The maximum repeating number is 3
7
The maximum repeating number is 3
12211

#include0using3 #include14

The maximum repeating number is 3
95#include16

#include54

The maximum repeating number is 3
7#include56#include57

C#

using7

The maximum repeating number is 3
95
The maximum repeating number is 3
92 #include20

using7#include01

The maximum repeating number is 3
92 #include24

The maximum repeating number is 3
5namespace6 #include27

#include28

The maximum repeating number is 3
92 #include30
The maximum repeating number is 3
44
The maximum repeating number is 3
45
The maximum repeating number is 3
46__

The maximum repeating number is 3
5
The maximum repeating number is 3
4

#include47

The maximum repeating number is 3
92 #include49#include50

using7* arr, 9

#include51

The maximum repeating number is 3
92
The maximum repeating number is 3
66

using #include59

maxRepeating(7 #include61

using7using3 using4

using7

The maximum repeating number is 3
4

The maximum repeating number is 3
21using8

The maximum repeating number is 3
21namespace0

using7namespace2

#include0namespace2

#include0namespace6 namespace7

The maximum repeating number is 3
5namespace2

The maximum repeating number is 3
5
The maximum repeating number is 3
35 int0
The maximum repeating number is 3
37
The maximum repeating number is 3
38

The maximum repeating number is 3
5
The maximum repeating number is 3
4

A

#include0____6

The maximum repeating number is 3
62

#include0____6

The maximum repeating number is 3
65
The maximum repeating number is 3
66
The maximum repeating number is 3
06

#include0using27using28

using29using30using31

using32using33

The maximum repeating number is 3
5namespace2

namespace2

PHP

using37

Is

The maximum repeating number is 3
4

The maximum repeating number is 3
5
The maximum repeating number is 3
6
The maximum repeating number is 3
7using50 using51using50using53using42using55using50using57

#include0using40#include30using40#include30using50using64using44using66using44

The maximum repeating number is 3
06

#include0using70 using71using40using73

#include0using75 using76

The maximum repeating number is 3
5
The maximum repeating number is 3
6
The maximum repeating number is 3
7using50 using81using50

The maximum repeating number is 3
5
The maximum repeating number is 3
4

#include0using3

The maximum repeating number is 3
7using40#include30using50using96using70using45

#include0

The maximum repeating number is 3
4

using7using70 using71using40#include30using50namespace07

using7using75 using71using50

The maximum repeating number is 3
06

#include0namespace2

The maximum repeating number is 3
5namespace2

The maximum repeating number is 3
5namespace6 using75
The maximum repeating number is 3
06

namespace2

The maximum repeating number is 3
5using40 using71namespace25namespace26

The maximum repeating number is 3
5using42 namespace29using40namespace31

The maximum repeating number is 3
5using44 namespace34

The maximum repeating number is 3
5namespace36 int7namespace38

#include0maxRepeating(using40

The maximum repeating number is 3
45using42
The maximum repeating number is 3
45using44namespace31

namespace47

JavaScript

namespace48

using38 namespace50

The maximum repeating number is 3
4

The maximum repeating number is 3
5
The maximum repeating number is 3
6 namespace54

#include0#include1

The maximum repeating number is 3
5namespace58

The maximum repeating number is 3
5
The maximum repeating number is 3
6 namespace61

The maximum repeating number is 3
5
The maximum repeating number is 3
4

#include0using3 using4

#include0

The maximum repeating number is 3
4

using7using8

using7namespace0

#include0namespace2

The maximum repeating number is 3
5namespace2

The maximum repeating number is 3
5namespace6 namespace7

namespace2

The maximum repeating number is 3
5namespace82

The maximum repeating number is 3
5namespace84

The maximum repeating number is 3
5namespace86

The maximum repeating number is 3
5namespace88int7
The maximum repeating number is 3
71

#include0namespace92namespace93namespace31

namespace95

Đầu ra

The maximum repeating number is 3

Độ phức tạp về thời gian: O (n) & nbsp; không gian phụ trợ: O (1) O(n) 
Auxiliary Space : O(1)

Bài tập: Giải pháp trên chỉ in một phần tử lặp lại và không hoạt động nếu chúng ta muốn in tất cả các phần tử lặp lại tối đa. Ví dụ: nếu mảng đầu vào là {2, 3, 2, 3}, giải pháp trên sẽ chỉ in 3. Điều gì sẽ xảy ra nếu chúng ta cần in cả 2 và 3 khi cả hai đều xảy ra số lần tối đa. Viết thời gian O (n) và O (1) Chức năng không gian bổ sung in tất cả các yếu tố lặp lại tối đa. . The above solution prints only one repeating element and doesn’t work if we want to print all maximum repeating elements. For example, if the input array is {2, 3, 2, 3}, the above solution will print only 3. What if we need to print both of 2 and 3 as both of them occur maximum number of times. Write a O(n) time and O(1) extra space function that prints all maximum repeating elements. (Hint: We can use maximum quotient arr[i]/n instead of maximum value in step 2).
Note that the above solutions may cause overflow if adding k repeatedly makes the value more than INT_MAX.  


Làm thế nào để bạn in một số lặp lại tối đa trong Python?

Khoa học dữ liệu thực tế sử dụng Python..
N: = Kích thước của A ..
Đối với tôi trong phạm vi 0 đến n, làm. A [a [i] mod k]: = a [a [i] mod k] + k ..
MAX_VAL: = A [0].
Kết quả: = 0 ..
Đối với tôi trong phạm vi 1 đến n, làm. Nếu một [i]> max_val, thì. MAX_VAL: = A [i] Kết quả: = I ..
Kết quả trở lại ..

Hàm nào trả về giá trị lặp lại tối đa?

C ++ sử dụng STD không gian tên; // Trả về phần tử lặp lại tối đa trong ARR [0..N-1].arr[0..n-1].

Làm thế nào để bạn tìm thấy phần tử lặp lại tối đa trong một mảng?

Chương trình 2: Tìm phần tử lặp lại tối đa trong một mảng..
Start..
Tuyên bố mảng ..
Khởi tạo mảng ..
Gọi chức năng sẽ trả về phần tử xảy ra nhất ..
Sắp xếp mảng đầu tiên ..
Đi qua mảng để đếm tần số của từng phần tử ..
Trả về phần tử với tần số cao nhất ..
In phần tử ..

Làm thế nào để bạn tìm thấy phần tử lặp lại tối đa trong một mảng trong Java?

Điều hướng mảng.Cập nhật mảng như cho ITH INDEX:- Arra [Arra [i]% n] = Arra [Arra [i]% n] + n;Bây giờ điều hướng mảng được cập nhật và kiểm tra chỉ mục nào có giá trị tối đa, số chỉ mục đó là phần tử có sự xuất hiện tối đa trong mảng.arrA[arrA[i]% n] = arrA[arrA[i]% n] + n; Now navigate the updated array and check which index has the maximum value, that index number is the element which has the maximum occurrence in the array.