Optimize pattern ID array merging code.

Only read a new value from the array that had the smallest ID, saving one
read per loop.

For duplicate removal, store previous value is a register to get faster
code and less cases to check.

Use pointers into the arrays and count down the number of values remaining
in each list.