Set集合不重复

    技术2024-10-05  51

    package com.example.file; import java.util.HashSet; import java.util.Set; public class Test { public static void main(String[] args) { // TODO Auto-generated method stub int[] nums = {1,2,3,43,3,6,1,2}; System.out.println(find(nums)); } public static int find(int[] nums) { Set<Integer> set = new HashSet<>(); int row = -1; for(int num:nums) { if (!set.add(num)) { //add添加失败,返回false 找到了某个重复元素 row = num; return num; } } return row; } }

    Set集合的特点:无序+不重复 本题主要利用Set集合不重复的特点

    Processed: 0.011, SQL: 9