Find the Duplicate in the array
Given an myArray write a JS program to find duplicate in the Array.
input1
[1,2,3,4,5,7,7,8]
output1
7
output1
{'light'', "dark", "twinlight"]
"use strict";
process.stdin.resume();
process.stdin.setEncoding("utf-8");
let inputString = "";
let currentLine = 0;
process.stdin.on("data", (inputStdin) => {
inputString += inputStdin;
});
process.stdin.on("end", (_) => {
inputString = inputString
.trim()
.split("\n")
.map((str) => str.trim());
main();
});
function readLine() {
return inputString[currentLine++];
}
function main() {
let myArray = JSON.parse(readLine().replace(/'/g, '"'));
/* Please do not modify anything above this line */
/* Write your code here */
}
"use strict";
process.stdin.resume();
process.stdin.setEncoding("utf-8");
let inputString = "";
let currentLine = 0;
process.stdin.on("data", (inputStdin) => {
inputString += inputStdin;
});
process.stdin.on("end", (_) => {
inputString = inputString.trim().split("\n").map((str) => str.trim());
main();
});
function readLine() {
return inputString[currentLine++];
}
function main() {
let myArray = JSON.parse(readLine().replace(/'/g, '"'));
/* Please do not modify anything above this line */
/* Write your code here */
myArray.sort();
let res = [];
for(let i = 0; i < myArray.length; i++) {
if(myArray[i] === myArray[i + 1]){
if(res[res.length - 1] !== myArray[i]){
res.push(myArray[i])
}
}
}
let resStr = '';
res.length > 0 ? res.map( (item, index) => index == 0 ? resStr += item : resStr +=`, ${item}`) : '';
console.info(resStr);
}
Comments
Leave a comment