JSFiddle - React, Tailwind, and code Playground

by Matthew Vasallo

HTML

<ul id="theList">
    <li>6</li>
    <li>3</li>
    <li>1</li>
    <li>4</li>
    <li>7</li>
    <li>5</li>
    <li>2</li>
    <li>8</li>
    <li>9</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.
*/
//you c


var theList = document.getElementById('theList');
var listItems = [];

for(var i = 0; i < theList.children.length; i ++){
    listItems.push(theList.children[i].innerHTML);
}

var test = [6,3,1,4,7,4,2,8,9,2];

function firstDuplicate(array){
    var tracker = {};
    
    for(var i = 0; i < array.length; i ++){
        if(!tracker[array[i]]){
            tracker[array[i]] = 1;
        } else {
            return "found first duplicate equal to " + array[i];
        }
    }
    
    return 'no duplicates';
};

alert(firstDuplicate(listItems));