An integer number algorithm to train the most (java)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/lc5801889/article/details/86765081

Problem Description

Write a program that reads a set of integers, which is the group of integers in the ascending order of the number N which is input by the user, up to no more than 20. The program then this will be an array of statistics, the highest number of occurrences of that array element value printed out. If two elements have the same number of value occurs, that is tied for first, then print only the relatively small value.
  Input format: The first line is an integer N, N £  20; Then there are N rows, each row represents an integer, and the ascending order in accordance.
  Output formats: Output only one line, that is, the element values most often appears.

Sample input and output

Sample input

		5
		100
		150
		150
		200
		250

Sample Output

		150

Thinking this question:
This problem mainly on the calculated number of duplicate data array, the number of first occurrence of each data array is calculated, to find the number of times to obtain the index corresponding to the maximum number of position, and then to re-array the corresponding position in the output .
Note:
1, must pay attention to the N range, the output is not smaller than N 1, the subject trap is provided.
2, the problem of profiling data to feel very normal, my program tested hundreds of times without problems, but still only had 90 percent of the time of submission, do not know where there is some question, please be below Gangster comments help me correct, the VIP students can help me look at what data the fifth evaluation point is, thank you!
code show as below:

public class Main{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		//注意取值范围,题目设有陷阱
        if (n < 1 || n > 20) {
            return;
        }
		long arr[] = new long[n+1];
		for (int i = 0; i < arr.length-1; i++) {
			arr[i]=sc.nextLong();
		}
		//由于最后一个数据也要计算次数,因此在数组末尾添加一个不相干数据进行对比
		arr[n]=-1;
		//计算数据出现的次数,使用动态数组方便增删改
		ArrayList<Integer> time = new ArrayList<Integer>();
		for (int i = 0; i < arr.length; i++) {
			int num = 1;
			for (int j = i+1; j < arr.length; j++) {
				if(arr[i]==arr[j]){
						num++;
					}else{
						time.add(num);
						i = j-1;
						break;
					}
				}
			}

		//计算次数最多的数据的索引位置
			int index = 0;
				for (int i = 0; i < time.size()-1; i++) {
					if(time.get(i)<time.get(i+1)){
						index=i+1;
					}
				}

		//创建一个动态数据将原数组数据导入方便去重处理
			ArrayList<Long> al = new ArrayList<Long>();
			for (int i = 0; i < arr.length; i++) {
				al.add(arr[i]);
			}
		//对动态数组去重
			for (int i = 0; i < al.size()-1; i++) {
				if(al.get(i).equals(al.get(i+1))){
					al.remove(i);					
				}
			}

			//打印对应索引位置元素
			System.out.println(al.get(index));
			}
		}

image

Guess you like

Origin blog.csdn.net/lc5801889/article/details/86765081