JAVA中求某个数组的众数?自己写了个,但是不对

Python020

JAVA中求某个数组的众数?自己写了个,但是不对,第1张

public

static

double

mode(double[]

array)

{

Arrays.sort(array)

int

count

=

1

int

longest

=

0

double

mode

=

0

for

(int

i

=

0

i

<

array.length

-

1

i++)

{

if

(array[i]

==

array[i

+

1])

{

count++

}

else

{

count

=

1//如果不等于,就换到了下一个数,那么计算下一个数的次数时,count的值应该重新符值为一

continue

}

if

(count

>

longest)

{

mode

=

array[i]

longest

=

count

}

}

System.out.println(longest)//打印出这个数出现的次数已判断是否正确

return

mode

}

我运行过了,是对的

import java.util.Scanner

public class P

{

public static void main(String[] args)

{

int i,j,t,len,max=1

int[][] d=new int[10][2]

Scanner sc=new Scanner(System.in)

d[0][0]=sc.nextInt()

d[0][1]=1

len=1

for(i=1i<10i++)

{

t=sc.nextInt()

for(j=0j<lenj++)

{

if(d[j][0]==t)

{

d[j][1]++

break

}

}

if(j>=len)

{

d[len][0]=t

d[len++][1]=1

}

if(d[j][1]>max)

max=d[j][1]

}

for(i=0i<leni++)

if(d[i][1]==max)

System.out.println(""+d[i][0]+" "+max)

sc.close()

}

}

public static void Main(String[] args)

{

System.out.println(ZhongSHu())

}