package shuzu_practice;

public class Practice3 {
/*
需求3:
有如下数组: {1,2,3,3,4,4,4,5,5,5,5,6,6,6,6,6}
统计数组中每个元素出现的数量
打印格式要求如下: 不能重复打印元素
数字1在数组中出现了1次
数字2在数组中出现了1次
数字3在数组中出现了2次
数字4在数组中出现了3次
数字5在数组中出现了4次
数字6在数组中出现了5次
不允许使用集合
*/
static int index = 0;

public static void main(String[] args) {
int[] oldArr = {1, 2, 3, 3, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 6};
int[] quchong = quchong(oldArr);

for (int i = 0; i < index; i++) {
int count = 0;
for (int j = 0; j < oldArr.length; j++) {
if (oldArr[j] == quchong[i]) {
count++;
}
}
System.out.println("数字" + quchong[i] + "在数组中出现了" + count + "次");

}

}

public static int[] quchong(int[] oldArr) {

int[] newArr = new int[oldArr.length];

newArr[0] = oldArr[0];
index = 1;//定义新数组的长度
for (int i = 1; i < oldArr.length; i++) {
boolean l = false;
for (int j = 0; j < index; j++) {
if (oldArr[i] == newArr[j]) {
l = true;
}
}
if (!l) {
newArr[index] = oldArr[i];
index++;
}
}
return newArr;
}
}