JQuery Remove duplicate elements – Easy method

JQuery Remove duplicate elements: It’s very easy to remove duplicate elements from a list using jQuery.

Demo:

Let’s say we have below list:

jquery remove duplicate elements

Here multiple duplicate values are there, Test 1, Test 3 and Test 2. Using jQuery we can easily remove them, refer the below code.

var seen = {};
$('p').each(function() {
    var txt = $(this).text();
    if (seen[txt])
         $(this).remove();
    else
	 seen[txt] = true;
});
  • seen is an object which maps any previously seen text to true.
  • Loop all p elements.
  • If any seen[txt] is exists then it will be removed using remove().

Full Code :

<!DOCTYPE html>
<html>
<head>
	<title>Remove duplicate Elements</title>
</head>
<body>
	<p>Test 1</p>
	<p>Test 2</p>
	<p>Test 1</p>
	<p>Test 3</p>
	<p>Test 4</p>
	<p>Test 3</p>
	<p>Test 1</p>
	<p>Test 5</p>
	<p>Test 2</p>
</body>
</html>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
	$(document).ready(function()
	{
		var seen = {};
		$('p').each(function() {
		    var txt = $(this).text();
		    if (seen[txt])
		        $(this).remove();
		    else
		        seen[txt] = true;
		});
	});
</script>

Include https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js for jQuery

Refer jQuery for more relatedsolutions.

Related Posts

jQuery sorting by price

jQuery sorting by price – Easy method(jQuery 3.x)

In this example let’s check how to do jQuery sorting by price in an easy way. Demo: Its a very important feature in ecommerce platforms to do…

sum of datatable column

Calculate sum of Datatable columns and display in footer – Easy method with sum()

In this example let’s check how to calculate sum of datatable columns (multiple columns) and display the same in footer. In some cases we may need to…

jquery datatable

jQuery Datatable – Easy Steps

Here is simple example to create jQuery Datatable. jQuery Datatable organizes data in grid like format of rows and columns. It is very organized, interactive and intutive….

jquery toggle

jQuery Toggle() Method

How to do jquery Toggle() method to show/hide onclick The jQuery toggle() method is used to toggle between the hide() and show() method. It shows the hidden…

jquery validation

jQuery validation for confirm password

How to validate password and confirm password using jQuery validation plugin ? Earlier we have discussed how to validate a form validation with jQuery validate plugin, in…

validate a form using jquery

Validate a form using jQuery

How to validate a form using jQuery or form validation using jQuery plugin ? We can validate a form using jQuery very simply and in an easy…

This Post Has 2 Comments

  1. I blog frequently and I seriously thank you for your information.
    The article has really peaked my interest. I am going to book mark
    your site and keep checking for new information about once a
    week. I opted in for your RSS feed too.

Leave a Reply

Your email address will not be published. Required fields are marked *