Chakradhar Jonagam Interview

by Matthew Vasallo

HTML

<ul id="theList">
    <li>6</li>
    <li>3</li>
    <li>1</li>
    <li>4</li>
    <li>7</li>
    <li>4</li>
    <li>2</li>
    <li>8</li>
    <li>9</li>
    <li>2</li>
</ul>

JavaScript

/*
Write an algorithm that will find the first duplicate in a list on the page.
For example we would return 4, for the list above.
*/

// arr  = [6,3,1,4,7,4,2,8,9,2];

$(document).ready(function(){
    var arrFound=[];
    $("#theList").find("li").each(function(i){
        if(arrFound[parseInt($(this).text())]==undefined){
           arrFound[parseInt($(this).text())]=true;
        }
        else{
            alert("First duplicate is "+ $(this).text());
            return false;
        }
    });
});