Skip to content

Instantly share code, notes, and snippets.

@MichaelWalker-git
Last active November 4, 2016 04:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MichaelWalker-git/453170bad9614edaded20a1bfa9966ac to your computer and use it in GitHub Desktop.
Save MichaelWalker-git/453170bad9614edaded20a1bfa9966ac to your computer and use it in GitHub Desktop.
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