Answer to Question #306941 in HTML/JavaScript Web Application for chethan

Question #306941

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

}


1
Expert's answer
2022-03-07T07:11:03-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 */

    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);
}

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