L
L
LebedevImagine2019-03-24 17:48:50
Algorithms
LebedevImagine, 2019-03-24 17:48:50

What is the best way to solve this simple problem?

There is a simple task, described below . In what way would you solve it? Please respond in the format I have described below. My idea is to create a two-dimensional array, but obviously I'm thinking wrong at the moment.
The input is N - the number of lines that will be entered into the program. Immediately after entering N, certain values ​​\u200b\u200bof K are entered - the student's identifier (ID). The value of K can be very large . If K is encountered with a specific value, such as 1043, then we must increase the student's personal variable Count by one.
Task: display the maximum Count and its corresponding ID.
Input example:
5
1029
1011
1029
8
78
Output:
2 1029
In other words, you need to determine which number occurs most often and display the value of how many times this number has occurred.
Questions
1) What is the best way to implement this task? Is it possible to do without creating a huge two-dimensional array?
2) What are the options for solving the problem in general, except for the most optimal one, described by you in paragraph 1)
UPD: It is obvious that the problem can be solved using dynamic data structures like hash tables or trees, as noted in the answer to the question. However, I'm interested in a solution using standard language tools without creating data structures. If such a solution is not possible, I will be glad to see your suggestions for a solution.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
@
@pestunov, 2019-03-24
@LebedevImagine

I propose the following algorithm:
1. Take a dynamic data structure that allows you to quickly add and search for elements (tree, hash table).
2. Add a repetition counter to each element.
3. Maintain two global variables: ID_MAX and Count_MAX.
4. Compare Count_MAX with the next updated counter and reassign ID_MAX and Count_MAX if the updated counter exceeds Count_MAX.
5, Output ID_MAX and Count_MAX.
PS And why is there a two-dimensional array?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question