Hand-written argmax loop
for i ..: if a[i] > a[j]: j = i: D.max(by: i => a[i]) names the index of the largest
let n: 1..=100 = read()
let a: [0..n]Int = read()
var j = 0
for i in 0..n:
if a[i] > a[j]:
j = i
print(j)let n: 1..=100 = read()
let a: [0..n]Int = read()
print((0..n).max(by: i => a[i])!)Why
C++ finds the index of the largest value by keeping a best index j and replacing it whenever a[i] > a[j]. (lo..hi).max(by: i => a[i]) says the same thing in one call and keeps the first largest on ties, exactly as the strict > loop does; over the whole table, a.argmax() is the index directly (argmin and min(by:) for <). The loop with >= keeps the last largest instead, so it is not reported. There is no automatic fix, since j's start value and later uses have to change with it.
The practice it serves
A hand-written pattern the language or the standard library already says in one step.
Not flagged
A near miss: close to the pattern, but not it.
let n: 1..=100 = read()
let a: [0..n]Int = read()
var j = 0
for i in 0..n:
if a[i] >= a[j]:
j = i
print(j)Turn it off
For one block, wrap it in with lint(loop_argmax: allow):. For a project, add loop_argmax = "allow" under [lints] in cp.toml.
Generated from spec/lints/loop_argmax, the same record the linter is built from.