# Line Counter

URL: https://hifreetools.com/text/line-counter

> Paste any text and instantly see how many total lines it has, plus a breakdown of non-empty, unique, and duplicate lines. Everything runs in your browser, so nothing is uploaded.

## Quick answer

To count lines, paste your text into the box. The tool splits the text on every newline character to get the total line count, then ignores blank lines for the non-empty count, uses a Set to find unique lines, and subtracts unique from non-empty to report duplicates. Results update live as you type.

## Formula

- Total lines: total = number of segments after splitting the text on the newline character
- Non-empty lines: nonEmpty = count of lines that contain at least one non-whitespace character after trimming
- Unique lines: unique = size of the Set built from all non-empty lines
- Duplicate lines: duplicates = nonEmpty - unique

The line counter splits your pasted text on the newline character (\n) to produce an array of lines, so the total line count equals the number of items in that array. A trailing newline therefore adds one empty segment to the total, and a blank line in the middle also counts toward the total. Non-empty lines are those that contain at least one non-whitespace character after trimming, so lines made up only of spaces or tabs are treated as empty. Unique lines are computed by inserting each non-empty line into a JavaScript Set, which keeps only one copy of each distinct value; the size of the Set is the unique count. Duplicate lines are then derived as non-empty minus unique, which tells you how many extra repeated lines exist beyond the first occurrence of each distinct line. All counting happens client-side in plain JavaScript, so even very large pastes stay private and never leave your device.

## Examples

- Simple list with a repeat and a blank line: apple banana apple orange → Total: 5, Non-empty: 4, Unique: 3, Duplicate: 1
- No duplicates: red green blue → Total: 3, Non-empty: 3, Unique: 3, Duplicate: 0
- Multiple repeats and whitespace-only line: cat cat cat dog → Total: 5, Non-empty: 4, Unique: 2, Duplicate: 2

## FAQ

**How does the line counter define a line?**
A line is any segment of text separated by a newline character. The tool splits your input on every newline, so the total line count is simply the number of segments produced, including blank lines that fall between content.

**What is the difference between total lines and non-empty lines?**
Total lines counts every segment, including blank or whitespace-only ones. Non-empty lines counts only those segments that still contain at least one visible character after trimming surrounding spaces and tabs.

**How are unique and duplicate lines calculated?**
Unique lines are the distinct non-empty lines, found by adding each non-empty line to a Set so repeats collapse to one. Duplicate lines is non-empty minus unique, which is the number of repeated lines beyond the first appearance of each distinct line.

**Does a trailing newline at the end of my text add an extra line?**
It adds one empty segment to the total because splitting on the newline character produces a blank final element. That blank does not affect the non-empty, unique, or duplicate counts, since whitespace-only and empty segments are ignored for those.

**Are whitespace-only lines counted as content?**
No. Lines that contain only spaces or tabs are trimmed to nothing and treated as empty, so they are included in the total count but excluded from non-empty, unique, and duplicate counts.

**Is my text uploaded anywhere?**
No. All counting runs entirely in your browser using plain JavaScript. Your text never leaves your device and nothing is sent to a server, which makes it safe for sensitive logs or lists.


## Sources

- [MDN: String.prototype.split()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split)
- [MDN: Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set)
- [Wikipedia: Newline](https://en.wikipedia.org/wiki/Newline)

Runs in the browser; nothing is uploaded. Cite as: HIFreeTools, "Line Counter", https://hifreetools.com/text/line-counter
