In this question i asked how to perform a conditional increment. The provided answer worked, but does not scale well on huge data-sets.
The Input:
<Users>
<User>
<id>1</id>
<username>jack</username>
</User>
<User>
<id>2</id>
<username>bob</username>
</User>
<User>
<id>3</id>
<username>bob</username>
</User>
<User>
<id>4</id>
<username>jack</username>
</User>
</Users>
The desired output (in optimal time-complexity):
<Users>
<User>
<id>1</id>
<username>jack01</username>
</User>
<User>
<id>2</id>
<username>bob01</username>
</User>
<User>
<id>3</id>
<username>bob02</username>
</User>
<User>
<id>4</id>
<username>jack02</username>
</User>
</Users>
For this purpose it would be nice to
- sort input by username
- for each user
- when previous username is equals current username
- increment counter and
- set username to ‘$username$counter’
- otherwise
- set counter to 1
- when previous username is equals current username
- (sort by id again – no requirement)
Any thoughts?
This transformation produces exactly the specified wanted result and is efficient (O(N)):
When applied on the provided XML document:
the wanted, correct result is produced: