
可以利用hashMap的方法来实现,步骤如下:
(视频教程推荐:java课程)
1、HashMap中的键存储数组array的数字,值存储array中的数值出现的个数;
2、遍历HashMap,找到Value值等于1的键,并将其储存在新数组temp中;
3、将数组temp里面的值赋值给num1,num2;
代码如下:
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
public class Solution {
public void FindNumsAppearOnce(int [] array,int num1[] , int num2[]) {
Map<Integer,Integer> map=new HashMap();
for(int i=0;i<array.length;i++){
if(map.containsKey(array[i])){
int len=map.get(array[i]);
map.put(array[i],len+1);
}else{
map.put(array[i],1);
}
}
int[] temp=new int[2];
int index=0;
Set<Map.Entry<Integer, Integer>> sm=map.entrySet();
for (Map.Entry<Integer, Integer> entry : sm) {
int t1=entry.getKey();
int t2=entry.getValue();
if(t2==1){
temp[index++] = t1;
}
}
num1[0]=temp[0];
num2[0]=temp[1];
}
}更多教程请访问java入门教程栏目。
以上就是如何找出整型数组中只出现一次的数字的详细内容,
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)