JavaScript - Remove Duplicate Values from Arraylist String


JavaScript - Remove Duplicate Values from Arraylist String

Mar 12, 2013
Introduction

Here I will explain how to use JavaScript to remove duplicate values from array or remove duplicate values/elements from string array using JavaScript

Description:

In previous article I explained Show number of characters left in text using JavaScriptSet watermark text for textbox using JavaScriptJavaScript Automatically move cursor to next field when textbox full ,JavaScript add bookmark link to website and many articles relating to JavaScriptjQuerySQLasp.netetc. Now I will explain how to remove duplicate values/elements from string array using JavaScript.

To remove duplicate values/elements from string array using JavaScript we need to write the code like as shown below


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Remove duplicates from an array using JavaScript </title>
<script type="text/javascript">
function RemoveList() {
var sampleArr = new Array(1, 1, 1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 4); //Sample array
uniqueArr(sampleArr);
}
//Adds new uniqueArr values to temp array
function uniqueArr(arr) {
arr1 = new Array();
for (i = 0; i < arr.length; i++) {
if (!checkstatus(arr1, arr[i])) {
arr1.length += 1;
arr1[arr1.length - 1] = arr[i];
}
}
alert(arr1);
}
// Check if it contains unique or duplicate record
function checkstatus(arr, e) {
for (j = 0; j < arr.length; j++) if (arr[j] == e) return true;
return false;
}
</script>
</head>
<body>
<div>
<input type="button" value="Remove Duplicates" onclick="RemoveList()" />
</div>
</body>
</html>
Live Demo

For live demo click on below button it will return only unique values from this string (1, 1, 1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 4) values



Comments

  1. Remarkable! Its really awesome article, I have got
    much clear idea regarding from this piece of writing.


    my weblog: http://trading.Goldgrey.org/

    ReplyDelete
  2. Yes! Finally someone writes about cash flow game.

    Look into my webpage: buying gold online

    ReplyDelete

Post a Comment