-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathShenCeInterview2.java
More file actions
45 lines (40 loc) · 1.04 KB
/
Copy pathShenCeInterview2.java
File metadata and controls
45 lines (40 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package other;
/**
* 神策数据 面试手写算法题2-统计数字个数
*
* 有数据:[1,2,3,4,3,2,1,4,5,6] 统计每个数字出现的个数,并输出每个数字出现的个数,格式如下:
* 1,2
* 2,2
* 3,2
*
* @author 刘壮飞
* https://github.com/zfman.
* https://blog.csdn.net/lzhuangfei.
*/
public class ShenCeInterview2 {
public void todo(int[] arr){
//get max of array
int max=arr[0];
for(int i=1;i<arr.length;i++){
if(arr[i]>max) max=arr[i];
}
// 下标保存数字
// 元素值保存的是次数
// java int[] 元素默认为0
int[] b=new int[max+1];
for(int i=0;i<arr.length;i++){
b[arr[i]]++;
}
for(int i=0;i<b.length;i++){
if(b[i]!=0){
System.out.println(i+","+b[i]);
}
}
}
public static void main(String[] args){
int[] a={
1,2,3,4,3,2,1,4,5,6
};
new ShenCeInterview2().todo(a);
}
}