(I'm not a lawyer. DO NOT rely on the content below as legal advice. Please consult an attorney for your serious business.)
Since you said
Although there will be slight differences, the code would likely end up looking similar to original because it's just the most efficient way to write that feature.
I will abstractly, without exploring into your factual circumstances, discuss the question of whether, in implementing a specific feature, it infringes copyright to use copyrighted software code that is the most efficient way to implement that feature. The discussion focuses on US copyright law.
(tl;dr; go to the bold paragraph at the bottom.)
Expression-Idea Dichotomy
Copyright only protects expression of ideas, but not the ideas themselves. For example, copying the content of a cookbook may result in copyright infringement, but simply cooking by following the procedure (idea) outlined in the cookbook generally does not infringe copyright. Simply speaking, by offering the authors limited-time protection over their expression, copyright is an engine that encourages people to generate expression. The ideas embodied in these expression, as implemented in the real world, benefits the society as a whole. Therefore, the purpose of copyright law, in some sense, is to encourage the implementation of ideas embodied in copyrighted expression.
Merger of Expression and Idea
But expression and ideas sometimes merge. When this happens, the idea (which has merged with its expression) is no longer protected by copyright law. For example, in Baker v. Selden, 101 U.S. 99 (1879), Selden described a book-keeping system in his book, which includes certain blank forms. To use the book-keeping system in Selden's book, copying these blank forms is inevitable. The Court holded that there was no copyright infringement and explained:
... But this object
would be frustrated if the knowledge could not be used without incurring the guilt of
piracy of the book. And where the art it teaches cannot be used without employing the
methods and diagrams used to illustrate the book, or such as are similar to them, such
methods and diagrams are to be considered as necessary incidents to the art, and given
therewith to the public; not given for the purpose of publication in other works
explanatory of the art, but for the purpose of practical application...
Merger of Expression and Idea in Software Code
When there is only one or very few ways to (efficiently) implement a certain idea in computer programming, such as an algorithm, the expression (the software code) and the idea (e.g., the algorithm), may have merged. The court in Computer Associates International, Inc. v. Altai, Inc., 982 F.2d 693 (2d Cir. 1992) explained:
While, hypothetically, there might be a myriad of ways in which a programmer may effectuate certain functions within a program, — i.e., express the idea embodied in a given subroutine — efficiency concerns may so narrow the practical range of choice as to make only one or two forms of expression workable options. See 3 Nimmer § 13.03[F][2], at 13-63; see also Whelan, 797 F.2d at 1243 n. 43 ("It is true that for certain tasks there are only a very limited number of file structures available, and in such cases the structures might not be copyrightable. . . ."). ... It follows that in order to determine whether the merger doctrine precludes copyright protection to an aspect of a program's structure that is so oriented, a court must inquire "whether the use of this particular set of modules is necessary efficiently to implement that part of the program's process" being implemented. [] If the answer is yes, then the expression represented by the programmer's choice of a specific module or group of modules has merged with their underlying idea and is unprotected. []