You are approaching this problem from an overly peremptory point of view.
In XQuery, you can set the values ββof variables, but you can never change them.
The correct way to execute iterative type algorithms is a recursive function:
declare funciton local:most($sequence, $index, $value, $count) { let $current=$sequence[$index] return if (empty($current)) then $value else let $current-count = count(index-of($current, $sequence)) return if ($current-count > $count) then local:most($sequence, $index+1, $current, $current-count) else local:most($sequence, $index+1, $value, $count) }
but the best way to approach the problem is to describe the problem in an inactive way. In this case, of all the different values ββin your sequence, you want to specify the one that displays the maximum number of times of any single value.
The previous post translated into XQuery,
let $max-count := max(for $value1 in distinct-values($sequence) return count(index-of($sequence, $value1))) for $value2 in distinct-values($sequence) where (count(index-of($sequence, $value2)) = $max-count return $value2
Oliver hallam
source share