Skip to content

Instantly share code, notes, and snippets.

@divmgl
Created December 1, 2015 06:09
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 divmgl/d7ec6299f25d822617d1 to your computer and use it in GitHub Desktop.
Save divmgl/d7ec6299f25d822617d1 to your computer and use it in GitHub Desktop.
PermCheck Javascript 100%/100%
function solution(A) {
var L = A.length; // Length of the array
var X = ((L + 1) * L) / 2; // Sum from 1..L
var Y = 0; // Sum of evaluated values
var I = 0; // Counter
var F = []; // Found elements
var V = -1; // Container
while (I < L) { // Start searching the array
V = A[I]; // Get the value
I++; // Increase the counter
if (F[V]) continue; // If we've accounted for this, continue
F[V] = true; // Record the value
Y += V; // Add to the evaluated sum
}
// If the evaluated sum is equal to the sum of the array length
// then we can be sure that the array is a permutation.
return X === Y ? 1 : 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment