0

I have following code for checking whether there is duplicate in an array. The code works fine. But it uses a new array named newUniqueArray. Is there a better code for this purpose without using a new array? Is there any optimization possible on this code?

Note: I have used inArray and in keywords from jQuery

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.4.1.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $('#btnSave').click(function (e) {
            var reportRecipients = "A, a , b,";
            reportRecipients = reportRecipients.toLowerCase();
            checkDuplicate(reportRecipients);
        });

        function checkDuplicate(reportRecipients) {
            if (reportRecipients.length > 1) {
                var recipientsArray = reportRecipients.split(',');
                var newUniqueArray = [];

                for (a in recipientsArray) {
                    var email = $.trim(recipientsArray[a]);

                    if ($.inArray(email, newUniqueArray) == -1) {
                        newUniqueArray.push(email);
                    }
                }

                if (newUniqueArray.length < recipientsArray.length) {
                    alert('Duplicate Exists');
                }

                return false;
            }
        }
    });
</script>
</head>
<body>
<input name="txtName" type="text" id="txtName" />
<input type="submit" name="btnSave" value="Save" id="btnSave" />
</body>
</html>
LCJ
  • 22,196
  • 67
  • 260
  • 418

2 Answers2

3

If you just want to test on string arrays, you can use a JavaScript object's property to test. It used a hash-table to look up properties, which is faster than array iteration.

example: http://jsfiddle.net/jmDEZ/8/

function checkDuplicate(reportRecipients) {
    var recipientsArray = reportRecipients.split(','),
        textHash = {};
    for(var i=0; i<recipientsArray.length;i++){
        var key = $.trim(recipientsArray[i].toLowerCase());
        console.log("lower:" + key);
        if(textHash[key]){
            alert("duplicated:" + key);
            return true;
        }else{
            textHash[key] = true;
        }
    }
    alert("no duplicate");
    return false;
}​
Chris Li
  • 3,715
  • 3
  • 29
  • 31
  • Thanks. What is the advantage of using `textHash` over `newUniqueArray` – LCJ Dec 12 '12 at 08:58
  • 2
    @Lijo, object property lookup is often implemented by hash-table(O(1)), while in array you query over iteration(O(n)). See this question: http://stackoverflow.com/questions/7700987/performance-of-key-lookup-in-javascript-object. Although, an object is not exactly a hash table, see this blog: http://www.devthought.com/2012/01/18/an-object-is-not-a-hash/. But at most time when you know what you are testing, it works. – Chris Li Dec 12 '12 at 09:17
2

I can't see any reason to use jQuery for this purpose:

checkDuplicate = function (reportRecipients) {
    if (reportRecipients.length > 1) {
        var recipientsArray = reportRecipients.split(',');
        for (a in recipientsArray) {
            if(reportRecipients.indexOf(a) != reportRecipients.lastIndexOf(a)){
                return true;
            }
        }
    }
    return false;
}

$('#btnSave').click(function (e) {
            var reportRecipients = "A, a , b,";
            reportRecipients = reportRecipients.toLowerCase();
            if(checkDuplicate(reportRecipients)) alert('Duplicate Exists');
        });
user666
  • 226
  • 1
  • 8