Last active
April 6, 2019 02:37
-
-
Save KyleMit/577d8dacff7a3586ba8280fea05f7ca4 to your computer and use it in GitHub Desktop.
String Characters - Group & Count
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
var myString = "aaAbbcdeffff".toUpperCase() | |
// potential bucket for one of every letter | |
var letters = [] // ex. ["a", "b", "c", "d"] | |
var counts = [] // ex. [3, 2, 1, 1] | |
// loop through every letter in string | |
for (i=0; i < myString.length; i++) { | |
var char = myString[i] | |
// see if character already exists in 'letters' array, and if so, we need to return the index | |
var charPos = -1 | |
for (j=0; j < letters.length; j++) { | |
var letterChar = letters[j] //ex. "a" | |
var valueAlreadyExists = char === letterChar | |
if (valueAlreadyExists) { | |
charPos = j | |
} | |
} | |
//var charPos = letters.indexOf(char) // ex. 1 | |
// see if character exists in letters | |
var valueAlreadyExists = charPos >= 0 | |
if (!valueAlreadyExists) { | |
// doesn't exist - insert property and set value to one | |
letters.push(char) | |
counts.push(1) | |
} else { | |
// already exists - increment value | |
// find position of char in letters array | |
counts[charPos] = counts[charPos] + 1 | |
} | |
} | |
console.log(letters,counts) |
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
var myString = "aaAbbcdeffff".toUpperCase() | |
// potential bucket for one of every letter | |
var letters = [] // ex. ["a", b", "c", "d"] | |
var counts = [] // ex. [3, 2, 1, 1] | |
// loop through every letter in string | |
for (i=0; i < myString.length; i++) { | |
var char = myString[i] | |
var charPos = letters.indexOf(char) // ex. 1 | |
// see if character exists in letters | |
var valueAlreadyExists = charPos >= 0 | |
if (!valueAlreadyExists) { | |
// doesn't exist - insert property and set value to one | |
letters.push(char) | |
counts.push(1) | |
} else { | |
// already exists - increment value | |
// find position of char in letters array | |
counts[charPos] = counts[charPos] + 1 | |
} | |
} | |
console.log(letters,counts) |
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
var myString = "aabc" | |
var chars = {} | |
for (i=0; i < myString.length; i++) { | |
var char = myString[i] | |
// see if i have an old value | |
var valueAlreadyExists = true //false | |
if (valueAlreadyExists) { | |
// already exists - increment value | |
chars[char] = chars[char] + 1 | |
} else { | |
// doesn't exist - insert property and set value to one | |
chars[char] = 1 | |
} | |
} | |
console.log(chars) |
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
[...myString].reduce((acc, char) => { | |
acc[char] = (acc[char] || 0) + 1 | |
return acc | |
}, {}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment