Javascript - Remove duplicates
avascript - Remove duplicates
CODE
<!--JQuery Reference Library-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">
</script>
<script>
function removeDuplicates(){
inputText = "Apple\nBanana\nOrange\nBanana\nBAnana"; // Provide the input text here.
var lines = inputText.split("\n"); //Split the input based on the new line character.
var resultArray = [];
//Loop through the splitted lines.
$.each(lines, function(){
if ($.inArray(String(this), resultArray) == -1) resultArray.push(String(this)); //Check if the line is present in the array. If present don't add it to the array again. Otherwise add it to the array.
});
var result = resultArray.join("\r\n"); //Join all the array elements with a new line character.
alert(result);
}
removeDuplicates();
//Output
//Apple
//Banana
//Orange
//BAnana
</script>
Comments
Post a Comment