Last active
November 4, 2016 04:01
-
-
Save MichaelWalker-git/453170bad9614edaded20a1bfa9966ac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function solution(X, A) { | |
// write your code in JavaScript (Node.js 0.12) | |
var count = new Array(X); | |
for ( var i=0; i < A.length; i++) { | |
var info = { | |
minute : i, | |
count : 1 | |
}; | |
//creates an object for the minute, count | |
if (!count[A[i]]) | |
//if there is no count element (count) that the input array element (A[i]) | |
count[A[i]] = info; | |
// we create that particular count[A[i]] to { minute: i, count: 1 } | |
else { | |
count[A[i]].count ++; | |
// it exists and then ends up increasing the count | |
if (count[A[i]].minute > info.minute) { | |
count[A[i]]. minute = info.minute; | |
// if the new minute value is larger than the original, reset | |
} | |
} | |
} | |
var latestTime = 0; | |
for ( var i=1; i < count.length; i++) { | |
if (!count[i]) { | |
return -1 | |
} else { | |
if (latestTime < count[i].minute ) { | |
latestTime = count[i].minute; | |
} | |
} | |
} | |
// if A is empty, returns 0 | |
// if count[i] iteration has a gap return -1 | |
// if count | |
return latestTime; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment