# find couple

{% tabs %}
{% tab title="JS" %}
```javascript
const {log} = console;

// if a+b == 9, define (a,b) a "couple"
Array.prototype.findCouple = function(){
    let mate = {};                // hash table
    for(const a of this){
        if(mate[a] !== undefined) return [a, mate[a]];
        mate[9-a] = a;
    }
    return null;
};

// ------------ log ------------
[
    [1,2,3,4,5,6].findCouple(),     // [ 5, 4 ]
    [1,2,3,4,6].findCouple(),       // [ 6, 3 ]
    [1,2,5,6,10].findCouple(),      // null

].forEach(x => log(x));
```
{% endtab %}

{% tab title="📗 參考 " %}
* [x] JavaScript Data Structures and Algorithms (2019), p.62
{% endtab %}
{% endtabs %}
