国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

JavaScript獲取兩個數(shù)組交集的方法

2019-11-20 12:19:36
字體:
供稿:網(wǎng)友

本文實例講述了JavaScript獲取兩個數(shù)組交集的方法。分享給大家供大家參考。具體如下:

這里傳入的數(shù)組必須是已經(jīng)排過序的

/* finds the intersection of  * two arrays in a simple fashion.  * * PARAMS * a - first array, must already be sorted * b - second array, must already be sorted * * NOTES * * Should have O(n) operations, where n is  *  n = MIN(a.length(), b.length()) */function arrayIntersection(a, b){ var ai=0, bi=0; var result = new Array(); while( ai < a.length && bi < b.length ) {   if (a[ai] < b[bi] ){ ai++; }   else if (a[ai] > b[bi] ){ bi++; }   else /* they're equal */   {    result.push(a[ai]);    ai++;    bi++;   } } return result;}console.log(arrayIntersection([1,2,3],[2,3,4,5,6]));//[2,3]

希望本文所述對大家的javascript程序設(shè)計有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 布尔津县| 金堂县| 清徐县| 建始县| 咸丰县| 剑河县| 鹿邑县| 葫芦岛市| 郸城县| 景谷| 黎川县| 佳木斯市| 新河县| 成武县| 吉安市| 井陉县| 丰台区| 阜南县| 东台市| 宜良县| 灌南县| 河南省| 平顶山市| 渑池县| 宜君县| 铁岭县| 开封市| 阳江市| 兰州市| 南木林县| 大英县| 揭东县| 容城县| 资中县| 梁河县| 板桥市| 繁昌县| 泰和县| 南通市| 罗田县| 澄江县|