我不怎么满意,实际效率也很低
nums1.sort(function (a,b) {return a-b;}); nums2.sort(function (a,b) {return a-b;}); function unique(nums){ res=[]; hash=[]; for(i=0;i<nums.length;i++){ if(hash[nums[i]]==undefined){ res.push(nums[i]); hash[nums[i]]=true; } } return res; } nums1=unique(nums1); nums2=unique(nums2); res=[]; if(nums1.length==0 || nums2.length==0){ return []; } if(nums1.length>0){ n1=nums1.shift(); } if(nums2.length>0) { n2 = nums2.shift(); } while((n1!=null && n2!=null) && (nums1.length>0 || nums2.length>0)){ if(n1==n2){ res.push(n1); n1=nums1.shift(); n2=nums2.shift(); continue; } if(n1>n2){ n2=nums2.shift(); continue; }else{ n1=nums1.shift(); continue; } } if(n1==n2){ res.push(n1); } return res;