JSFiddle - React, Tailwind, and code Playground

by Alexander Wigmore

HTML

<div data-productid="1" class="product-item">1</div>
<div data-productid="2" class="product-item">2</div>
<div data-productid="1" class="product-item">3</div>

<div id="submitform">Click</div>

JavaScript

function eliminateDuplicates( A ) { // finds any duplicate array elements using the fewest possible comparison
		var i, j, n;
		var d;
		n=A.length;
		// to ensure the fewest possible comparisons
		for (i=0; i<n; i++) { // outer loop uses each item i at 0 through n
			for (j=i+1; j<n; j++) { // inner loop only compares items j at i+1 to n
				if (A[i]==A[j]) {console.log(A[i]);
					el = $('*[data-productid="'+A[i]+'"]');
					console.log(A[j]);
					el.css({"background":"red"});
					d="no";
				}
				else
				{
					if(d!="no"){d="yes"};
				}
			}
		}
		return d;
	}
	

	
	$('#submitform').click(function(e){
	var a = [];
		$(".product-item").each(function(){
		  a.push($(this).data('productid'));
		});
		$x=eliminateDuplicates(a);
		if($x=="yes")
		{
			$('#formid').submit();
		}
		else
		{
			e.preventDefault();
			alert("Duplicate entry of same product twice");
		}
	});