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>
4
  • Please read on how to use jsfiddle properly. Also, check the syntax highlighting. Commented Dec 12, 2012 at 8:17
  • You need to put your js code in the js frame and choose a proper framework(JQuery) on the left pane. See jsfiddle.net/jmDEZ/5, this is a updated version of your code and it works Commented Dec 12, 2012 at 8:20
  • What does your array consist of? Commented Dec 12, 2012 at 8:39
  • @badZoke Array consists of strings Commented Dec 12, 2012 at 9:39

2 Answers 2

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;
}​
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. What is the advantage of using textHash over newUniqueArray
@Lijo, object property lookup is often implemented by hash-table(O(1)), while in array you query over iteration(O(n)). See this question: stackoverflow.com/questions/7700987/…. Although, an object is not exactly a hash table, see this blog: devthought.com/2012/01/18/an-object-is-not-a-hash. But at most time when you know what you are testing, it works.
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');
        });

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.