File tree Expand file tree Collapse file tree 1 file changed +17
-9
lines changed Expand file tree Collapse file tree 1 file changed +17
-9
lines changed Original file line number Diff line number Diff line change 9
9
'use strict' ;
10
10
11
11
12
- /*
12
+ /**
13
13
* Computes the set difference of two arrays.
14
14
*
15
15
* @param {array } a
16
16
* @param {array } b
17
- * @returns all elements of a that are not in b.
17
+ * @returns out all elements of a that are not in b.
18
18
* If a is not an array, an empty array is returned.
19
19
* If b is not an array, a is returned.
20
20
*/
21
21
function difference ( a , b ) {
22
- if ( ! Array . isArray ( a ) ) {
23
- return [ ] ;
22
+ if ( ! Array . isArray ( a ) ) return [ ] ;
23
+ if ( ! Array . isArray ( b ) ) return a ;
24
+
25
+ var hash = { } ;
26
+ var out = [ ] ;
27
+ var i ;
28
+
29
+ for ( i = 0 ; i < b . length ; i ++ ) {
30
+ hash [ b [ i ] ] = 1 ;
24
31
}
25
- if ( ! Array . isArray ( b ) ) {
26
- return a ;
32
+
33
+ for ( i = 0 ; i < a . length ; i ++ ) {
34
+ var ai = a [ i ] ;
35
+ if ( ! hash [ ai ] ) out . push ( ai ) ;
27
36
}
28
- return a . filter ( function ( e ) {
29
- return b . indexOf ( e ) < 0 ;
30
- } ) ;
37
+
38
+ return out ;
31
39
}
32
40
33
41
module . exports = {
You can’t perform that action at this time.
0 commit comments