严氏北美IT公司面试真题汇总和解答论坛
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Intersection Of Two ArraysI WayI

Go down

Intersection Of Two ArraysI WayI Empty Intersection Of Two ArraysI WayI

Post by Admin Sat Oct 21, 2017 2:18 pm

public int[] intersection(int[] nums1, int[] nums2) {
   HashSet<Integer> set1 = new HashSet<Integer>();
   for(int i: nums1){
       set1.add(i);
   }
 
   HashSet<Integer> set2 = new HashSet<Integer>();
   for(int i: nums2){
       if(set1.contains(i)){
           set2.add(i);
       }
   }
 
   int[] result = new int[set2.size()];
   int i=0;
   for(int n: set2){
       result[i++] = n;
   }
 
   return result;
}

Admin
Admin

Posts : 124
Join date : 2017-10-21

https://csinterviewquestions.forumotion.com

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum