Need help with displaying an alert message in JavaScript for duplicate values in an array?
var names = [];
var nameInput = document.getElementById("txt1");
var messageBox = document.getElementById("display");
function insert() {
names.push(nameInput.value);
clearAndPush();
}
function clearAndPush() {
nameInput.value = "";
messageBox.innerHTML = "";
messageBox.innerHTML += "Names:" + names.join(", ";
function removeDups(names) {
let unique = {};
names.forEach(function (i) {
if (!unique[i]) {
unique[i] = true;
}
});
return Object.keys(unique);
}
document.getElementById("display").innerHTML = removeDups(names);
<label>Name:</label><input type="text" id="txt1" placeholder="Enter Name">
<input type="button" value="Click" onclick="insert()">
<div id="display"></div>