Last active
August 29, 2015 14:04
-
-
Save jikeytang/d033e1fd914fa9b5e392 to your computer and use it in GitHub Desktop.
[ Javascript ] - 20140716-题目1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
找出以下2个数组中相同数的最大一个数 | |
比如: | |
var A = [1,3,4,5,6,7,8,9]; | |
var B = [12,10,9,8,7,1]; | |
相同的有: | |
1,7,8,9 | |
那最大的一个数就是9。 | |
PS: | |
1. 回复时注意加上下面这句话,才会有语法高亮或格式缩进。 | |
```javascript | |
// you code | |
``` | |
2. 粘贴代码时请使用shift+tab,缩进前面的空白。 |
sliwey-zz
commented
Jul 16, 2014
var A = [1,3,4,5,6,7,8,9];
var B = [12,10,9,8,7,1];
var i, j, max, UD, l = A.length, len = B.length;
for (i = 0; i < l; i++)
for (j = 0;j < len; j++)
B[j] === A[i] && (B[j] > max || max === UD) && (max = B[j]);
var A = [1,3,4,5,6,7,8,9];
var B = [12,10,9,8,7,1];
var re = [];
A.sort().reverse();
A.map(function(x){
B.indexOf(x) !== -1 && re.push(x);
})
console.log(re) //重复数
console.log(re[0]) //重复数中的最大数
var arr1=[1,3,4,5,6,7,8,9];
var arr2=[12,10,9,8,7,1];
var arr3=[];
for(var s in arr1){
for(var x in arr2){
if(arr1[s]==arr2[x]){
arr3.push(arr1[s]);
}
}
}
console.log("相同的元素有"+arr3)
对Array扩展一个交集的方法intersection
Array.prototype.intersection = function(){
var arrs = Array.prototype.slice.call(arguments);
return this.filter(function (item) {
return arrs.every(function (otherArr) {
return otherArr.indexOf(item) != -1;
});
});
};
var A = [1,3,4,5,6,7,8,9,9];
var B = [12,10,9,8,7,1];
var C = A.intersection(B)
C.sort(function (a,b) {
return b > a;
});
console.log(C[0]);
var A = [1, 3, 4, 5, 6, 7, 8, 9];
var B = [12, 10, 9, 8, 7, 1];
var l = A.length+B.length;
A = A.concat(B).sort(function(a,b){return a-b});
while (--l) {
if (A[l] == A[l - 1]) break;
}
console.log(A[l]);//max
var arr1=[1,2,3,4,5];
var arr2=[2,3,4,5,6];
function getMax(arr1,arr2){
var arr3=arr1.concat(arr2);
var arr3=arr3.sort();
var r=[];
for(var i=0;i<arr3.length;i++){
if(arr3[i]==arr3[i+1]){
r.push(arr3[i]);
}
}
return r[r.length-1];
}
console.log(getMax(arr1,arr2));
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment