Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mcsheffrey/6964981 to your computer and use it in GitHub Desktop.
Save mcsheffrey/6964981 to your computer and use it in GitHub Desktop.
function findObjPropInArr(arr, prop, val) {
for(var i = 0; i<arr.length; i++) {
if(arr[i][prop] === val) {
return i;
}
}
return -1;
}
function findFirstNoRepeat(word) {
var wordCount = [],
arrIndex,
i,
r;
for(i = 0; i<word.length; i++) {
arrIndex = findObjPropInArr(wordCount, 'letter', word.charAt(i));
if (arrIndex > -1) {
wordCount[arrIndex].count++;
} else {
wordCount.push({
letter: word.charAt(i),
count: 1
});
}
}
console.log(wordCount);
for(r = 0; r<wordCount.length; r++) {
if(wordCount[r].count === 1) {
console.log(wordCount[r].letter)
return wordCount[r].letter
}
}
}
findFirstNoRepeat('teeter');
findFirstNoRepeat('total');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment