Judge the odd and even number of occurrences of each number in the numerical array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | <!DOCTYPE html>
<html>
<head>
<meta charset= "utf-8" >
<title>数组操作</title>
</head>
<body>
<script type= "text/javascript" >
var arr=[3,1,2,2,1,3,1];
var sum=[];
var res=[];
var count =0;
var temp;
for ( var i=0;i<arr.length;i++){
if (res.indexOf(arr[i])==-1){
res.push(arr[i]);
}
}
for ( var i=0;i<res.length;i++){
for ( var j=0;j<arr.length;j++){
if (arr[j]==res[i]){
count ++;
}
}
sum.push( count );
count =0;
}
console.log(res);
for ( var i=0;i<res.length;i++){
var str=(sum[i]%2==0)? "偶数" : "奇数" ;
console.log(res[i]+ "出现了" +sum[i]+ "次" );
console.log(res[i]+ "出现了" +str+ "次" );
}
</script>
</body>
</html>
|
Copy after login
Alibaba written test - array operation - find the different elements in the two arrays
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <script type= "text/javascript" >
function diff(arr1,arr2){
var ress = [];
var arr = arr1.concat(arr2);
for ( var i=0,len=arr.length;i<len;i++){
if ((arr1.indexOf(arr[i])>=0 && arr2.indexOf(arr[i])<0) || (arr1.indexOf(arr[i])<0 && arr2.indexOf(arr[i])>=0)){
ress.push(arr[i]);
}
}
return ress;
}
var arr1 = [1,2,3,5,7,6];
var arr2 = [1,2,5];
var res = diff(arr1,arr2);
console.log(res);
</script>
|
Copy after login
Array deduplication
Method 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <!DOCTYPE html>
<html>
<head>
<meta charset= "utf-8" >
<title>数组去重01</title>
</head>
<body>
<script type= "text/javascript" >
Array.prototype.unique = function (){
var arr = [];
for ( var i=0,i=this.length;i<len;i++){
if (arr.indexOf(this[i]) == -1){
arr.push(this[i]);
}
}
return arr;
};
console.log([1,2,3,2,5,6,3].unique());
</script>
</body>
</html>
|
Copy after login
Method 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <!DOCTYPE html>
<html>
<head>
<meta charset= "utf-8" >
<title>数组去重02</title>
</head>
<body>
<script type= "text/javascript" >
Array.prototype.unique = function (){
var n = {},
r=[];
for ( var i = 0; i < this.length; i++)
{
if (!n[this[i]])
{
n[this[i]] = true;
r.push(this[i]);
}
}
return r;
}
console.log([1,2,3,2,5,6,3].unique());
</script>
</body>
</html>
|
Copy after login
Method 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <!DOCTYPE html>
<html>
<head>
<meta charset= "utf-8" >
<title>数组去重</title>
</head>
<body>
<script type= "text/javascript" >
Array.prototype.unique = function (){
var arr = [this[0]];
for ( var i=1;i<this.length;i++){
if (this.indexOf(this[i]) == i){
arr.push(this[i]);
}
}
return arr;
}
console.log([1,2,3,4,2,3,4].unique());
</script>
</body>
</html>
|
Copy after login
Method 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <!DOCTYPE html>
<html>
<head>
<meta charset= "utf-8" >
<meta http-equiv= "X-UA-Compatible" content= "IE=edge" >
<title>filter</title>
<link rel= "stylesheet" href= "" >
</head>
<body>
<script type= "text/javascript" >
var arr = [4,5,3,2,3,4,5,1];
function fn(num){
var res = num.filter( function (item,index, array ){
return num.indexOf(item) === index;
});
return res;
}
console.log(fn(arr));
</script>
</body>
</html>
|
Copy after login
Method 5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <!DOCTYPE html>
<html>
<head>
<meta charset= "utf-8" >
<meta http-equiv= "X-UA-Compatible" content= "IE=edge" >
<title>Object 对象来当做哈希表</title>
<link rel= "stylesheet" href= "" >
</head>
<body>
<script type= "text/javascript" >
function unique(a) {
var obj = {};
return a.filter( function (item) {
return obj.hasOwnProperty(item) ? false : (obj[item] = true);
});
}
var a = [1, 1, 3, 2, 1, 2, 4];
var res = unique(a);
console.log(res);
</script>
</body>
</html>
|
Copy after login