Answer to Question #219577 in HTML/JavaScript Web Application for sai

Question #219577

Find the Duplicate in an Array

Given an array

myArray, write a JS program to find the duplicate item in the array.Input

  • The input will be a single line containing an array myArray

Output

  • The output should be a single line containing the duplicate item or false if no duplicate item is present in the myArray

sample input 1

['light', 'dark', 'twilight']


sample ouput 1

false


sample input 2

[1, 2, 3, 4, 5, 6, 7, 7, 8]


sample output 2

7


1
Expert's answer
2021-07-22T18:43:20-0400
var con = "true"
var arr = [1, 2, 3, 4, 5, 6, 7, 7, 8];
print(arr)
var arr1 = []
for (var i = 0; i < arr.length; i++) {
    if(arr[i]==arr[i+1]){
        //print(arr[i]);
        arr1.push(arr[i])
        
    }
    
}


if(arr1.length===0){
    print("false")
}
else{
    print(arr1)
}


This is the link to see how the code runs is: https://onlinegdb.com/oRAwJTC_b





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