As part of our research, we've reconstructed and analyzed the evolution of Stack Overflow posts. In the course of that research, I extracted clones of code blocks from the official Stack Exchange data dump released 2018-09-05 and built a small website listing clones with at least 20 lines of code that are present in at least 20 different Stack Overflow threads.
I qualitatively analyzed the top 50 clones in that list and was able to identify the source (or at least a source) of the snippets in most of the cases. Moreover, I checked if the Stack Overflow posts refer to each other, which was rarely the case. In one example, a Java snippet was found in 45 different Stack Overflow threads and could have been copied from this website. It is not clear if this external source or this Stack Overflow post is the original source of the snippet. The post on androidhive was created around May 2012, the first occurrence on Stack Overflow was mid April 2012. Assuming that androidhive is the original source, the usage on Stack Overflow could be problematic (see their terms of service). If the original source is Stack Overflow, the androidhive author did not adhere to Stack Overflow's CC BY-SA license. I identified four more variants of this snippet on Stack Overflow (1, 2, 3, 4).
Another example is this VBA script, used in 21 different Stack Overflow posts and copied from here. The first occurrence attributes this source, but I didn't find any indication on the linked website that redistribution of that snippet is allowed.
I don't want to discuss when a snippet is copyrightable here, because there are already discussions and blog posts on that topic (1, 2, 3, 4). I also don't want to discuss the usage of code snippets from Stack Overflow on other platforms (I conducted research on that, too). What I want to discuss are the following two problems:
Duplicates of (non-trivial) code snippets within Stack Overflow:
The duplicates within Stack Overflow could indicate that different threads solved a similar problem. However, if there is no link between those threads, the information is spread and hard to capture for readers. Would it make sense to point Stack Overflow users to related threads based on the similarity of the code blocks posted in a thread? This could be done before users post a question or integrated into the website for existing posts.
EDIT: This snippet is present in 31 different posts, most of them created by the same author. I suggested an edit pointing from a later answer to the original post, but the author of the snippet rejected the edit. I would appreciate your feedback on this matter. Is copying the same snippet into different answers really considered best practice? Wouldn't it be better to just point to the original post? That way, only one snippet needs to be kept up-to-date (bug fixes, improvements, etc.).
Duplicates of (non-trivial) code snippets copied from external sources into Stack Overflow:
In the example above, the (possible) external source had a strict policy regarding the reuse of content. However, I also found copies of code snippets from the official Android documentation on Stack Overflow (example), which is licensed under CC BY 2.5, requiring attribution. Since I found many posts not referring to the source of the snippet, even those duplicates could be problematic. Leaving the copyright and licensing issues aside, Stack Overflow recommends to "always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline". But does it really make sense to copy such a code snippet (at least 14 times) from the Android reference documentation into Stack Overflow posts?
EDIT: I suggested edits adding the missing attribution for three posts using the above-mentioned Android snippet (1, 2, 3).
I'm aware that non-trivial is a vague description, I just wanted to make clear that I'm not talking about short inline snippets, but whole code blocks of a certain size. Obviously, those code blocks may contain log files, config files, or simple examples, but my list also contains enough example of Java, PHP, or VBA scripts like the ones mentioned above.