diff --git a/src/include/lib/qunique.h b/src/include/lib/qunique.h
index 4d620f8..fc539ca 100644
--- a/src/include/lib/qunique.h
+++ b/src/include/lib/qunique.h
@@ -30,8 +30,9 @@ qunique(void *array, size_t elements, size_t width,
 
 	for (i = 1, j = 0; i < elements; ++i)
 	{
-		if (compare(bytes + i * width, bytes + j * width) != 0)
-			memcpy(bytes + ++j * width, bytes + i * width, width);
+		if (compare(bytes + i * width, bytes + j * width) != 0 &&
+			++j != i)
+			memcpy(bytes + j * width, bytes + i * width, width);
 	}
 
 	return j + 1;
@@ -55,8 +56,9 @@ qunique_arg(void *array, size_t elements, size_t width,
 
 	for (i = 1, j = 0; i < elements; ++i)
 	{
-		if (compare(bytes + i * width, bytes + j * width, arg) != 0)
-			memcpy(bytes + ++j * width, bytes + i * width, width);
+		if (compare(bytes + i * width, bytes + j * width, arg) != 0 &&
+			++j != i)
+			memcpy(bytes + j * width, bytes + i * width, width);
 	}
 
 	return j + 1;
