我可以找到仅读取输入而不添加到数组的多数元素吗?我的代码在大输入时不起作用,数字差异很大。
我发现我的错误。有正确的代码:
int n = Integer.parseInt(bin.readLine()); // read number of data
int h = 0; //input data
int count = 1; //counter
int lf = 0; // last top counting
int first = 0; // top counter num
for (int x = 0; x < n; x++) {
lf = h;
h = Integer.parseInt(bin.readLine());//read input number
if (x == 0) {
first = h;
}
if (h == first) {
count++;
} else {
count--;
}
if (count == 0) {
first = lf;
count = 1;
}