JS取两个数组相同的元素详解编程语言

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]

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/8749.html

(0)
上一篇 2021年7月18日
下一篇 2021年7月18日

相关推荐

发表回复

登录后才能评论