Skip to main content
2 of 2
make code more efficient by searching only for the first occurrence rather than all
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k

Well here's a way. Find the position of the first occurrence of x:

expr = HoldForm[x + 2 + 4 + x];

pos = Position[expr, x, -1, 1];

Then:

ReplacePart[expr, pos -> 4]

4 + 2 + 4 + x

RunnyKine
  • 33.3k
  • 3
  • 110
  • 176