Answer to Question #307443 in HTML/JavaScript Web Application for king

Question #307443

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

input1

{'light'', "dark", "twinlight"]

output2

false


"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 */

}



1
Expert's answer
2022-03-07T10:26:54-0500
"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 */
let clearArr = [...new Set(myArray)]


let duplicates = [...myArray]
clearArr.forEach((item) => {
  const i = duplicates.indexOf(item)
  duplicates = duplicates
    .slice(0, i)
    .concat(duplicates.slice(i + 1, duplicates.length))
})
if (duplicates.length > 0) {
  for (let i=0; i<duplicates.length; i++) {
    console.log(duplicates[i])}
}
else {
  console.log(false)
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS