public class RepeatNum {
public static void main(String[] args) {
int[] arr = {2,3,1,0,2,5,3};
Integer res = func(arr);
System.out.println(res);
}
private static Integer func(int[] arr) {
if (arr == null || arr.length == 0) {
return null;
}
if (arr.length == 1) {
return arr[0];
}
Map<Integer, List<Integer>> map = new HashMap<>();
for (int i : arr) {
List<Integer> val = map.computeIfAbsent(i, key -> new ArrayList<>());
if (val.size() > 0) {
return val.get(0);
}
val.add(i);
}
return null;
}
}
computeIfAbsent
1、key不存在:
- 添加k-v
- 方法返回本次put的v
2、key存在:
- 不添加
- 方法返回此存在的key对应的v