现在假设我有一个数组
[1,2,3,4,5,3,3,1,2,1,1,6,7,5,7]
现在我想统计其中出现次数为1的值的数组
结果为[4,6]
请问有什么(高效&&优雅)的写法么?谢谢
自己写的总感觉不太好看...
[1,2,3,4,5,3,3,1,2,1,1,6,7,5,7]
现在我想统计其中出现次数为1的值的数组
结果为[4,6]
请问有什么(高效&&优雅)的写法么?谢谢
自己写的总感觉不太好看...
调试欢乐多
参考这个不过显示时你得反向了,呵呵!
将数组中的数字作为key,其出现次数作为value进行数组遍历,遍历完后打印出所有value为1的key即可。
import java.util.*;public class FindMostInArray {
public static void main(String args[]){
int[] intArray = {1,2,2,3,4,4,5,6,7,7,8,9};
List<Integer> list = new ArrayList<Integer> ();
String str = "";
for(int i=0; i<intArray.length; i++){
str += String.valueOf(intArray[i]);
} int MaxCount = 0;
for(int i=0; i<intArray.length; i++){
String tempString = str.replaceAll(String.valueOf(intArray[i]), "");
int tempCount = str.length() - tempString.length();
if (tempCount > MaxCount){
list.clear();
list.add(intArray[i]);
MaxCount = tempCount;
}
else if((tempCount == MaxCount) && (!list.contains(intArray[i]))){
list.add(intArray[i]);
}
}
System.out.println("The most repeat number is: "+ MaxCount +"\nThe duplicated elements are: "+ list.toString());
}
}
The most repeat number is: 2
The duplicated elements are: [2, 4, 7]
import java.util.Iterator;public class OnlyOne {
public static void main(String[] args) {
int[] arr = {1,2,3,4,5,3,3,1,2,1,1,6,7,5,7};
HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
for(int i=0; i<arr.length; i++){
hm.put(arr[i], hm.get(arr[i])==null?1:hm.get(arr[i])+1);
}
Iterator<Integer> iter = hm.keySet().iterator();
for(int i=iter.next(); iter.hasNext(); i=iter.next()){
if(hm.get(i) == 1){
System.out.println(i);
}
}
}
}
hist[arr[i]]=hist[arr[i]]+1;
可能我说得不够清楚吧
我仅仅希望得到其中的一组出现次数为特定值的数组而已
就比如[1,2,3,4,5,3,3,1,2,1,1,6,7,5,7]
我只需要得到[4,6]而已
而且这个东西我需要调用很多很多次,所以我希望它有足够的效率,而且我认为出现了Collection或者排序都是降低效率的做法,所以不可取
在这个算法中,效率最重要,空间不需要限制,因为要计算的都是一些很短的数组(长度50以内)
而且要统计的只是50以内的数字而已,我需要的只是这样一个特定的算法,并不需要兼容其它东西...最后,最好可以写得漂亮一点啦,看起来心情也愉快..
呵呵,望各位高手多多指教,谢谢
觉得不好的话,可以把HashMap改成int[50],然后参考7楼
import java.util.Arrays;public class Test { public static void main(String[] args) {
int[] ta = {1, 2, 3, 4, 5, 3, 3, 1, 2, 1, 1, 6, 7, 5, 7};
int[] r = test(ta, 1);
System.out.println(Arrays.toString(r));
} public static int[] test(int[] array, int count) {
if (array == null || array.length == 0 || count <= 0) return null;
int max = array[0];
for (int i = array.length - 1, j = 0; i > 0;) {
j = array[i--];
if (j < 0) return null;
if (max < j) max = j;
}
int[] t = new int[max + 1];
for (int i = array.length - 1; i >= 0;) {
t[array[i--]]++;
}
int[] r = new int[max + 1];
int j = 0;
for (int i = t.length - 1; i >= 0; i--) {
if (t[i] == count) r[j++] = i;
}
return Arrays.copyOf(r, j);
}}
输出:
[6, 4]
int count(int[] a)
{for(int i,j;i<a.length;i++)
{
int j=0;
if(a[i]==1)
{j++;}
}
return j;
}
public static int[] get1(int[] input) {
// 由于只是编计50以内的东西
// flag代表第i个数字出现了几次
// temp用来存放只出现一次的数字 int[] flag = new int[51];
int[] temp = new int[51]; // 第1次遍历
for (int i : input)
flag[i] += 1; // 第2次遍历,找出只出现一次的数字
int index = 0;
for (int i = 0; i < flag.length; i++) {
if (flag[i] == 1) {
temp[index++] = i;
}
} // 数组拷贝,返回确切的数组
int[] result = new int[index];
System.arraycopy(temp, 0, result, 0, index);
return result;
}