Problem Analysis:
Now that the return value is not duplicated, we might as well put the result in the set and compare the two set.
Problem solving:
Public classSolution {/** * @paramnums1 an integer array *@paramnums2 an integer array *@returnAn integer array*/ Public int[] Intersection (int[] nums1,int[] nums2) {Set<Integer> Set1 =NewHashset<integer>(); Set<Integer> Set2 =NewHashset<integer>(); List<Integer> list =NewArraylist<integer>(); for(inti = 0; i < nums1.length; i++) {Set1.add (nums1[i]); } for(inti = 0; i < nums2.length; i++) {Set2.add (nums2[i]); } Iterator<Integer> it =Set2.iterator (); while(It.hasnext ()) {intnum2 =It.next (); if(Set1.contains (num2)) {List.add (num2); } } int[] Inter =New int[List.size ()]; for(inti = 0; I < list.size (); i++) {Inter[i]=List.get (i); } returnInter; }}
Computes the intersection of two numbers (one)