
可以利用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入门教程栏目。
以上就是如何找出整型数组中只出现一次的数字的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号