---
title: "Use histogram diff for repeated lines in C#"
url: https://recursiveai.org/onediff/cookbook/diff-repeated-lines-with-histogram/
description: "Use histogram diff in C# to compare source code with repeated lines. See how matching less common lines changes which edits the diff shows."
version: "1.0.1"
---

# Use histogram diff for repeated lines in C#

**OneDiff** is a free, open-source diff library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneDiff) · [GitHub](https://github.com/recursive-ai-oss/onediff-csharp) · [Docs](https://recursiveai.org/onediff/docs/) · [Download example (.zip)](https://recursiveai.org/onediff/cookbook/diff-repeated-lines-with-histogram/diff-repeated-lines-with-histogram.zip)

Histogram diff uses less common lines to find matches in repeated code.

```shell
dotnet add package RecursiveAI.OneDiff
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using OneDiff;
string log = "Log();\n", flush = "Flush();\n";
string before = flush+log+log+log+log+flush;
string after = log+flush+log+log+flush+log;
Console.Write(Diff.Unified(before, after,
    algorithm: DiffAlgorithm.Histogram));
```

Unzip [the example](https://recursiveai.org/onediff/cookbook/diff-repeated-lines-with-histogram/diff-repeated-lines-with-histogram.zip), then `dotnet run`.

Prints a line diff that keeps both `Flush();` calls as unchanged context:

```text
--- a
+++ b
@@ -1,6 +1,6 @@
+Log();
 Flush();
 Log();
 Log();
-Log();
-Log();
 Flush();
+Log();
```

A leading `+` is an addition, a `-` is a deletion, and a space marks an unchanged line. `@@ -1,6 +1,6 @@` means the diff covers six lines starting at line 1 on each side.

## Variations

**Compare two source files.** Use these lines in place of the `before` and `after` declarations. Pass the old and new paths in that order: `dotnet run -- before.txt after.txt`. Use your own files; the example in the zip needs no input files.

```csharp
string before = File.ReadAllText(args[0]);
string after = File.ReadAllText(args[1]);
```

If either path doesn’t exist, `File.ReadAllText` throws before the comparison runs.

**Compare the same lines with patience diff.** Replace the print statement, keeping the inputs:

```csharp
Console.Write(Diff.Unified(before, after,
    algorithm: DiffAlgorithm.Patience));
```

## Related recipes

- [Use patience diff for source code](https://recursiveai.org/onediff/cookbook/diff-lines-with-patience/)
- [Write a line diff as Markdown](https://recursiveai.org/onediff/cookbook/write-line-diff-markdown-report/)

## The short answer

Pass `DiffAlgorithm.Histogram` as the `algorithm` in `Diff.Unified` when you want less frequent lines to guide the match, even if none of those lines is unique. In this example every line is either `Log();` or `Flush();`. Histogram can match the two `Flush();` calls and show the change as a rearrangement of the surrounding `Log();` calls. The diff reports added and deleted lines, not a separate move operation.

## How does histogram diff work?

OneDiff trims any identical start and end from the range it’s comparing, then counts occurrences of each line in the remaining **old** range. It looks for a shared line it can use as a starting point and extends the match in both directions while the lines agree. The rarest line in that match gives the match its score. A lower count wins; ties favor a longer match, then the earlier one in the new range, then the earlier one in the old range. The chosen match divides the comparison into two smaller ranges, where the same rules apply again.

This is a count of entire lines, not of individual words or C# statements. For example, `    Flush();` and `Flush();` aren’t equal lines, and changing whitespace can change the anchors. If the only lines shared by a range all occur more than 64 times in the old range, OneDiff uses Myers for that range. If the ranges share nothing, the result is a deletion and an insertion. See the [pinned histogram algorithm notes](https://recursiveai.org/onediff/docs/diff-algorithms/#histogram) for the exact rules.

## What’s the difference between histogram and patience diff?

Patience starts with lines that occur exactly once in each range. It chooses anchors that stay in the same order on both sides, and uses Myers when it can’t find a unique matching line in a range. Histogram can start from a line that occurs more than once. That matters in the example because `Flush();` occurs twice and `Log();` four times in each input, so patience has no unique line to anchor the initial range.

Running the patience variation prints:

```text
--- a
+++ b
@@ -1,6 +1,6 @@
-Flush();
-Log();
 Log();
+Flush();
 Log();
 Log();
 Flush();
+Log();
```

Both outputs describe the same change. The histogram output keeps both `Flush();` lines as context; the patience output shows one as deleted and reinserted. Neither result means a specific call was moved at runtime: identical lines have no separate identity in a line diff. If your review depends on which call belongs to which method, read the surrounding code rather than inferring that from a `+` or `-` alone.
