views:

36

answers:

2

Hello,

I've got a sequence of values. They can all be equal... or not. So with XQuery I want to get the most frequent item in the sequence.

let $counter := 0, $index1 := 0 
for $value in $sequence 
if (count(index-of($value, $sequence))) 
then 
{ 
$counter := count(index-of($value, $sequence)) $index1 := index-of($value) 
} else {} 

I can't make this work, so I suppose I'm doing something wrong.

Thanks in advance for any help you could give me.

+1  A: 

You are approaching this problem from too much of an imperative standpoint.

In XQuery you can set the values of variables, but you can never change them.

The correct way to do iterative-type algorithms is with 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 a better way of approaching the problem is by describing the problem in a non-iterative way. In this case of all the distinct values in your sequence you want the one that appears maximum number of times of any distinct value.

The previous sentance translated into XQuery is

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
Thank you so much, I've tried your way and also works.
deb
+2  A: 

Use:

  for $maxFreq in 
           max(for $val in distinct-values($sequence)
                     return count(index-of($sequence, $val))
               )
   return
      distinct-values($sequence)[count(index-of($sequence, .)) eq $maxFreq]
Dimitre Novatchev
Thank you so much, I think my inexperience had led me to try a very twisted approach.
deb