File tree 1 file changed +14
-7
lines changed
1 file changed +14
-7
lines changed Original file line number Diff line number Diff line change @@ -15,17 +15,24 @@ export function uniq<A>(a: A[]): A[] {
15
15
16
16
/**
17
17
* Removed all duplicates from the list based on the hash function.
18
+ * First element matching the hash function wins.
18
19
*/
19
- export function uniqueBasedOnHash < A > ( list : A [ ] , elementToHash : ( a : A ) => string ) : A [ ] {
20
- const hashSet = new Set ( )
20
+ export function uniqueBasedOnHash < A extends Record < string , any > > (
21
+ list : A [ ] ,
22
+ elementToHash : ( a : A ) => string ,
23
+ __result : A [ ] = [ ] ,
24
+ ) : A [ ] {
25
+ const result : typeof list = [ ]
26
+ const hashSet = new Set < string > ( )
21
27
22
- return list . reduce ( ( accumulator , currentValue ) => {
23
- const hash = elementToHash ( currentValue )
28
+ list . forEach ( element => {
29
+ const hash = elementToHash ( element )
24
30
if ( hashSet . has ( hash ) ) {
25
- return accumulator
31
+ return
26
32
}
27
33
hashSet . add ( hash )
34
+ result . push ( element )
35
+ } )
28
36
29
- return [ ...accumulator , currentValue ]
30
- } , [ ] )
37
+ return result
31
38
}
You can’t perform that action at this time.
0 commit comments