用类似这种的方法 不用Map
int[] a = new int[10]{1,2,3,4,5,6,7,7,7,7};
public int getPopularElement(int[] a)
{
int count = 1, tempCount;
int popular = a[0];
int temp = 0;
for (int i = 0; i < (a.length - 1); i++)
{
temp = a[i];
tempCount = 0;
for (int j = 1; j < a.length; j++)
{
if (temp == a[j])
tempCount++;
}
if (tempCount > count)
{
popular = temp;
count = tempCount;
}
}
return popular;
}
我想让this.method = 那个出现次数最多的数字 如果所有数都只出现一次的话就this.method = -1
或者出现次数相同的话也是this.method = -1 。
我照着上面的方法写的没法判断每个数只出现一次的情况 还有出现次数相同的情况
求帮忙看看怎么写
public Long getMode() {
Vector vector = cloned();
long [] num = vector.getElements();
// 要写的地方
return this.method;
}