Last active
June 29, 2017 19:00
-
-
Save Flanker4/f07ef4d72fdb2cb9749894235990bb43 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Place any jQuery/helper plugins in here. | |
function findUniq(arrayStr) { | |
var firstElement = arrayStr[0] | |
var secondElement = arrayStr[1] | |
var result = charSet(firstElement); | |
if (!isEqualSet(result, charSet(secondElement))) { | |
var thirdElement = arrayStr[2] | |
if (isEqualSet(result, charSet(thirdElement))) { | |
return secondElement; | |
} else { | |
return firstElement; | |
} | |
} | |
arrayStr = arrayStr.slice(2); | |
res = arrayStr.find((element) => { | |
var tmpSet = charSet(element); | |
var tmpResult = isEqualSet(result, tmpSet) | |
return !tmpResult | |
}); | |
return res; | |
} | |
function charSet(string) { | |
//Use memoization for this function | |
if (!charSet.cache) | |
charSet.cache = {}; | |
if (!charSet.cache[string]) { | |
charSet.cache[string] = new Set(string.replace(/ /g, '').split('')); | |
} | |
return charSet.cache[string] | |
}; | |
function isEqualSet(setA, setB) { | |
if (setA.size !== setB.size) { | |
return false; | |
} | |
for (let value of setA) { | |
if (!setB.has(value)) { | |
return false; | |
} | |
return true; | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment