当前位置:网站首页>Array to determine any duplicate value

Array to determine any duplicate value

2022-06-10 10:33:00 InfoQ

1、 Background
Judge any duplicate value of the array , Give you an array of integers nums, If any value appears in the array at least twice , return true; If each element in the array is different from each other , return false
2、 Code implementation
public class Solution {

 public static void main(String[] args) {
 int[] nums = new int[]{-2, 1, -3, 4, -1, 2, 1, -5, 4};
// System.out.println(containsDuplicate(nums));
 System.out.println(containsDuplicate(nums));
 }

 /**
 *  Give you an array of integers nums, If any value appears in the array at least twice , return true; If each element in the array is different from each other , return false
 *
 * @param nums
 * @return
 */
 public static boolean containsDuplicate(int[] nums) {
 Set set = new HashSet();
 for (int i : nums) {
 set.add(i);
 }
 return nums.length > set.size() ? true : false;
 }
}
3、 Result display
true

Process finished with exit code 0

原网站

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101012163129.html