Find the Duplicate in an Array
Given an array
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
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
Comments
Leave a comment