0%

LCR120

问题描述

设备中存有 n 个文件,文件 id 记于数组 documents。若文件 id 相同,则定义为该文件存在副本。请返回任一存在副本的文件 id

示例 1:

1
2
输入:documents = [2, 5, 3, 0, 5, 0]
输出:0 或 5

提示:

  • 0 ≤ documents[i] ≤ n-1
  • 2 <= n <= 100000

题解

hashset判断是否重复

1
2
3
4
5
6
7
8
9
10
class Solution {
public int findRepeatDocument(int[] documents) {
Set<Integer> hashset = new HashSet<>();
for (int i: documents){
if(hashset.contains(i)) return i;
hashset.add(i);
}
return -1;
}
}