publicintlongestConsecutive(int[] nums){//判断数组中是否有数据if(nums.length==0){return0;}//创建一个set,目的是去重HashSet<Integer> set =newHashSet<>();//将数组排序Arrays.sort(nums);//定义长度int length =1;//定义最大值int max =1;//遍历数组for(int i =1;i<nums.length;i++){if(!set.add(nums[i]))continue;if(nums[i-1]+1==nums[i]){
length++;
max=Math.max(max,length);}else{
length =1;}}return max;}