Skip to content

Instantly share code, notes, and snippets.

@amaseda
Created February 14, 2017 15:04
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 amaseda/61475372bed498de2eb60e98b3232862 to your computer and use it in GitHub Desktop.
Save amaseda/61475372bed498de2eb60e98b3232862 to your computer and use it in GitHub Desktop.
code-wars-challenges-solutions.md

Count Characters in a String

.forEach

function count(string) {  
  history = {}{
  string.split("")
        .forEach(char => {
          if(history[char]){
            history[char]++
          } else {
            history[char] = 1
          }
        })
  return history
}

.reduce

function count(string) {  
  return string.split("")
               .reduce((acc, curr) => {
                 acc[curr] ? acc[curr]++ : acc[curr] = 1
                 return acc
               }, {})
}

Delete Occurrences of an Element after N Repetitions

.filter

function deleteNth(arr,x){
  let counter = {}
  return arr.filter((val) => {
    counter[val] ? counter[val]++ : counter[val] = 1
    if(counter[val] <= x) {
      return val
    }
  })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment