# Hashing

<table><thead><tr><th width="118">Method</th><th width="224">Set</th><th width="307">Map</th><th>Big O</th></tr></thead><tbody><tr><td>Init</td><td><code>new Set(['1','2']);</code></td><td><code>new Map([ [0,1],[0,2] ]);</code></td><td></td></tr><tr><td>增加</td><td><code>mySet.add('1')</code></td><td><code>myMap.set(0, 1)</code></td><td>O(1)</td></tr><tr><td>刪除</td><td><code>mySet.delete('1')</code></td><td><code>myMap.delete(0)</code></td><td>O(1)</td></tr><tr><td>清掉</td><td><code>mySet.clear()</code></td><td><code>myMap.clear()</code></td><td></td></tr><tr><td>取值</td><td>-</td><td><code>mySet.get(0) // 1</code></td><td>O(1)</td></tr><tr><td>是否存在</td><td><code>mySet.has('1')</code></td><td><code>myMap.has(0)</code></td><td>O(1)</td></tr><tr><td>長度</td><td><code>mySet.size</code></td><td><code>myMap.size</code></td><td></td></tr><tr><td>轉陣列</td><td><code>[...mySet]</code></td><td><code>[...myMap.values()]</code></td><td></td></tr></tbody></table>

## Set

```javascript
// ES6 Set
let instruments = new Set();
// or 一開始就把值存進去
let instruments = new Set(['1','2']);


instruments.add('piano');
instruments.has('guitar'); // false
instruments.delete('guitar'); // 裡面根本沒有 guitar 所以回傳 false
instruments.size; // 1
[...instruments] //['piano']

// or use Array.from, 
Array.from(instruments); 
```

## Map

```javascript
let myMap= new Map();

// or 一開始就存值，記得是 [[]] double array
let myMap = new Map([[keyString ,'value']]);

var keyString = 'I am string',
    keyObj = {},
    keyFunc = function() {},
    keyNumber = 1

// 增加
myMap.set(keyString , 'string value');
myMap.set(keyObj, {obj: 1});
myMap.set(keyFunc , function(){console.log('I am function')});
myMap.set(keyNumber , 100);

// 有幾個
myMap.size; // 4

// 取值
myMap.get(keyObj); // {obj: 1}

// 看是否存在
myMap.has(keyString ); //  true

// 刪掉
myMap.delete(keyNumber); 
myMap.size; // 3

// 轉陣列
[...myMap.values()] // ["string value", {obj: 1}, ƒ]
```

## 其他

```javascript
if( lookup.has(str) ){
    lookup.set(str, lookup.get(str) + 1 )
} else {
    lookup.set(str, 1 )
}
```

可以改寫更簡短

```javascript
lookup.set(str, (lookup.get(str) ?? 0) + 1 )
```

以下同理

```javascript
if( !lookup.has(sortStr) ){
    lookup.set(sortStr, [str] )
} else {
     lookup.set(sortStr, [...lookup.get(sortStr), str] )
}

// 可以變以下
lookup.set(sortStr, [...(lookup.get(sortStr) ?? []), str] )
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hannahpun.gitbook.io/leetcode-note/tools/hashing.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
