# @lazy-array/util-unique

> 提供陣列去重的工具函式，包含 indexOf 和 Set 兩種實現方式 / Provides array deduplication utilities with indexOf and Set implementation methods

Latest version **1.0.7** (published 2026-03-15) · ISC license · 0 weekly downloads

## Install

```sh
npm install @lazy-array/util-unique
pnpm add @lazy-array/util-unique
yarn add @lazy-array/util-unique
bun add @lazy-array/util-unique
```

## Health

**Score 60/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 1.0.7 |
| Published | 2026-03-15 |
| First published | 2022-09-26 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 27.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | bluelovers |
| Maintainers | bluelovers |
| Keywords | create-by-yarn-tool, create-by-tsdx |

## Links

- npm: https://www.npmjs.com/package/@lazy-array/util-unique
- Repository: https://github.com/bluelovers/ws-array
- Homepage: https://github.com/bluelovers/ws-array/tree/master/packages/@lazy-array/util-unique#readme
- Issues: https://github.com/bluelovers/ws-array/issues
- npm.io page: https://npm.io/package/@lazy-array/util-unique

## Recent versions

- 1.0.7 (latest) — 2026-03-15
- 1.0.5 — 2024-03-09
- 1.0.4 — 2022-10-25
- 1.0.3 — 2022-10-14
- 1.0.2 — 2022-09-26
- 1.0.1 — 2022-09-26

## README

# @lazy-array/util-unique

提供陣列去重的工具函式，包含兩種實現方式。

Provides array deduplication utilities with two implementation methods.

## 功能說明 / Features

- **array_unique_indexOf**: 使用 indexOf 過濾方式取得唯一值 / Get unique values using indexOf filter
- **array_unique_by_set**: 使用 Set 資料結構取得唯一值 / Get unique values using Set data structure

## 安裝 / Install

```bash
yarn add @lazy-array/util-unique
yarn-tool add @lazy-array/util-unique
yt add @lazy-array/util-unique
```

## 使用範例 / Usage Examples

### array_unique_indexOf

使用 indexOf 過濾，保留第一個出現的元素。

Uses indexOf filter, keeping the first occurrence of each element.

```typescript
import { array_unique_indexOf } from '@lazy-array/util-unique';

// 基本使用
const result = array_unique_indexOf([1, 2, 2, 3]);
console.log(result); // => [1, 2, 3]

// 字串陣列
const strings = array_unique_indexOf(['a', 'b', 'a', 'c']);
console.log(strings); // => ['a', 'b', 'c']
```

### array_unique_by_set

使用 Set 資料結構，高效能去重。

Uses Set for high-performance deduplication.

```typescript
import { array_unique_by_set } from '@lazy-array/util-unique';

const result = array_unique_by_set([1, 2, 2, 3]);
console.log(result); // => [1, 2, 3]

const objects = array_unique_by_set([{a: 1}, {a: 1}, {b: 2}]);
console.log(objects); // => [{a: 1}, {a: 1}, {b: 2}] - 物件無法透過 Set 去重 / Objects cannot be deduplicated by Set
```

## 應用情境 / Application Scenarios

1. **資料預處理**: 在處理 API 回傳資料前去除重複項目
2. **表單驗證**: 檢查使用者輸入的重複值
3. **資料分析**: 計算唯一值數量
4. **陣列合併**: 合併多個陣列並去除重複

## 比較 / Comparison

| 方法 / Method | 效能 / Performance | 適用場景 / Use Case |
|-------------|-------------------|-------------------|
| array_unique_indexOf | O(n²) | 簡單類型、需保留第一個位置 |
| array_unique_by_set | O(n) | 大多數場景、高效能需求 |

## API 參考 / API Reference

[查看完整 API](index.d.ts)

---
_Source: https://npm.io/package/@lazy-array/util-unique · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
