public int findMoreRecurrentValue(List<Integer> sortedList) {
if(sortedList.size() == 0) return -1;
int mostRecurrent = -1;
int nReccurrences = 0;
int n = 1;
int current = sortedList.get(0);
for(int i = 1; i < sortedList.size(); ++i) {
if(sortedList.get(i) == current)
++n;
else {
if(n > nReccurrences) {
mostRecurrent = current;
nReccurrences = n;
}
current = sortedList.get(i);
n = 1;
}
}
// Check again at the end, the most reccurrent value could be the last one.
if(n > nReccurrences) {
mostRecurrent = current;
nReccurrences = n;
}
return mostRecurrent;
}
# 1 楼答案
# 2 楼答案
你可以使用
HashMap<index,count>
遍历链表 如果你发现相同的数字,那么增加计数 最后检查哪个计数是大的,并返回其索引