Q:

jQuery group elements by name with count

I have a large list of names from a database that I want to filter and give a count for a specific name value - in this case 'Anonymous'.
For example:
<ul>
    <li>Johnny Ace</li>
    <li>Mary Adams</li>
    <li>Bob Allen</li>
    <li>Anonymous</li>
    <li>Anonymous</li>
    <li>Anonymous</li>
    <li>Anonymous</li>
    <li>Anonymous</li>
    <li>David Brown</li>
    <li>Sue Smith</li>
    <li>John Williams</li>
</ul>

I want the result to be:
<ul>
    <li>Johnny Ace</li>
    <li>Mary Adams</li>
    <li>Bob Allen</li>
    <li>Anonymous (5)</li>
    <li>David Brown</li>
    <li>Sue Smith</li>
    <li>John Williams</li>
</ul>

What's the best way to do this?

A:

Something like this using :contains() to get the length and remove all but the first one, and then set the text
var count=$('ul li:contains("Anonymous")').length;
$('ul li:contains("Anonymous")').not(':first').remove().end().text("Anonymous("+count+")");

DEMO

