国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 冀州市| 抚远县| 博罗县| 原阳县| 崇州市| 赣州市| 彰化市| 呼和浩特市| 邵阳市| 盈江县| 湘潭市| 宣化县| 灵璧县| 陆川县| 武山县| 孟津县| 夏津县| 义马市| 丘北县| 昭苏县| 铜梁县| 河南省| 晋中市| 金寨县| 岐山县| 泸西县| 吉木乃县| 乐亭县| 北碚区| 米泉市| 扬中市| 扶风县| 锦屏县| 怀宁县| 晋江市| 诏安县| 襄城县| 广元市| 白玉县| 偏关县| 顺昌县|