jQuery Remove Duplicates

This is a useful jQuery Script that removes duplicates within an element such as a list. It simply cycles through an element and removes anything previously ‘seen’.

jQuery(document).ready(function() {
// remove duplicates
var seen = {};
$('ul li').each(function() {
var txt = $(this).text();
if (seen[txt])
$(this).remove();
else
seen[txt] = true;
});
});

Let’s take a list of Cities as an example:-

  • London
  • Paris
  • Berlin
  • London (is removed as duplicate)
  • Dublin
  • Brussels

In this example we are cycling through a standard unordered list and removing duplicates, but you are recommended to give the list / element a class as otherwise you could be targeting other elements on the page where you want duplicates.

About the Author
jQuery Remove Duplicates 0da77002febbfe28d15144a57bbcc2c9 s 180 d mm r g

Glenn

A highly experienced Web Designer / Web Developer (Webflow, WordPress, Weebly & Custom), Front-end / Back-end Developer & New Media Specialist, with extensive knowledge of a wide spectrum of technologies in the Development and Creative Industries, built up over a number of years.