Code similarity detection methods, compared
Every code plagiarism checker answers the same question, do these programs share more than chance allows?, in one of a handful of ways. Here is how each method works, what it catches, what it misses, and why good tools combine several.
2-day free trial. Checks every file against classmates, the web and AI, in 65+ languages.
count.pycomparedRename every variable and this token stream does not change. That is why renaming never hides a copy from a structural checker, and why rewriting the statements themselves sometimes does.
Text comparison
The simplest method compares files line by line, like diff, or looks for shared phrases, like an essay checker such as Turnitin. It is fast and easy to explain.
It is also the easiest to defeat. Renaming variables, reformatting or rewriting comments changes nearly every line of text and none of the program. Text comparison is useful for spotting identical files, and little else.
Token fingerprints (MOSS)
Token-based methods first reduce each program to a stream of tokens, as in the example above: keywords and punctuation stay, names and comments go. Then they compare the streams.
MOSS does it with winnowing: it hashes every short run of tokens, keeps a representative sample of those hashes as the file's fingerprints, and reports pairs that share many of them.
- Catches: renaming, reformatting, new comments, moved functions.
- Misses: statements rewritten into equivalent forms, which change the token runs. What MOSS misses shows one.
Greedy string tiling (JPlag)
JPlag also works on tokens, but instead of sampling fingerprints it searches for the longest runs the two programs share and tiles both programs with them, longest first. The score is how much of each program the shared tiles cover.
- Catches: the same edits as fingerprints, with precise match boundaries.
- Misses: the same thing: rewrites that break up long token runs.
Syntax trees and graphs
Instead of a flat stream, these methods parse each program into a syntax tree, or into a graph of which statements depend on which. Dolos, for example, parses code with tree-sitter before fingerprinting it.
- Catches: more restructuring. Reordering independent statements or swapping loop forms changes the text and tokens but leaves much of the tree or dependency graph intact.
- Costs: more computation per pair, and a parser for every language.
Learned models
Newer approaches train a model to turn each program into a numeric representation, so programs that do the same thing land close together even when their text and tokens differ. This is what makes some cross-language matches possible: a Python solution and its Java translation can look alike to a model when they look nothing alike as tokens.
Codequiry's Zeus engine is built on this idea. Like any model, it gives a score to review, not a verdict.
Beyond the class
Every method above compares the programs you give it with each other. Two more checks cover what that cannot see:
- Web search. Each program is compared with public code on GitHub, Stack Overflow, Software Heritage and the indexed web, and with earlier terms. This finds a copy even when no classmate made the same one.
- AI-authorship detection. A classifier looks at how the code is written rather than what it matches, because AI output is usually different for every student. How AI code detection works.
All of these in one check
Structure, web sources and AI, with the matched lines side by side.
2-day free trial
Side by side
| Method | Ignores renaming | Survives restructuring | Sees outside the batch |
|---|---|---|---|
| Text comparison | No | No | No |
| Token fingerprints | Yes | Partly | No |
| Greedy string tiling | Yes | Partly | No |
| Syntax trees and graphs | Yes | Better | No |
| Learned models | Yes | Better, across languages | No |
| Web search | Depends on the matcher | Depends on the matcher | Yes |
| AI detection | Answers a different question: who wrote it, not what it matches | ||
In practice, the method matters less than the combination. For how the tools compare, see MOSS vs Dolos vs JPlag, and for putting it to use, how to detect code plagiarism.
Frequently asked questions
How does code similarity detection work?
Most tools turn each program into a sequence of tokens or a syntax tree, which drops names, comments and formatting, and then look for long shared stretches between programs. The score reflects how much of each program is shared.
What algorithm does MOSS use?
Winnowing. MOSS hashes short runs of tokens into fingerprints, keeps a representative subset of them, and reports the pairs of files that share the most.
What algorithm does JPlag use?
Greedy string tiling over token sequences. It looks for the longest shared runs of tokens first and covers as much of both programs as it can with them.
Why does text comparison fail for code?
Because the text is the part students change first. Renaming variables or reformatting a file changes almost every line of text while leaving the program exactly the same.
Can similarity detection find AI-written code?
Not on its own. AI-written code is usually different for every student, so it matches nothing. Detecting it takes a separate classifier that looks at how the code is written.
Which method is the most accurate?
None wins everywhere. Token methods are fast and hard to fool with renaming; tree and graph methods survive more restructuring but cost more; web search and AI detection cover what comparison inside a class cannot see. Good tools combine them.
See the methods work on real code
Upload a batch and check it against each other, the web and AI.
Start free trial2-day free trial
Just one assignment? Run a single check