Please feedback longest_streak

Hi, i try suggestion solved for longest_streak because solution not work with me

def longest_streak(str)

current = “”
longest = “”
hash_new =
i = 0
while i < str.length + 1
if str[i] == str[i - 1] || i == 0
current += str[i]
hash_new[current.length] = current
current = str[i]
i += 1
sorted = hash_new.sort_by {|k,v|k}

return sorted.last[1]
thank you please i want feedback

Hey I wasn’t sure what project you where referring to, but this problem seems to work best based off of what it looks like you’re trying to accomplish

require 'byebug'
def longest_streak(str)
    hash = {} # you want an empty hash as usual 
    i = 0 #of course start off at 0 
        while i < str.length - 1
            #recommend adding debugger right there to see what the computer is doing

            # you gotta remember it is -1 not + 1
            #you don't have a value of str[str.length + 1], instead your
            #last number is str[str.length - 1], therefore, so you don't
            #get an error for nil value, you minize it by 1 so you can 
            #have a proper count that won't compare the last number to nil
            #for example if have a str = "hello" and we iterate each character
            #with i, our last i should be i = 4, which will be str[i] = "o"
            #but there is no i + 1 which will be 5, for i 4 to compare to 
            #therefore if we set up the while loop to end at length - 2, we can 
            #get i to only go up to 3, allowing i  to be 3 (str[3] = "l") to 
            #compare to i being 4 (str[4] = "o").  
            count = 1
            x = i + 1 
            until str[i] != str[x] #i love using until, but if works as well
                #i prefer 'until' because it stops the program once it reaches
                # true, I found it to be a better alternative to if, and has given 
                #me better results in certain problems
                count += 1
                x += 1 # we want to count 
                hash[str[i]] = count  
                i = x
        sorted = hash.sort_by {|k,v| v}

I defiantly recommend you using or go back to the course outline of byebug and pry.
With byebug you would be able to see what your program is doing, and what went wrong, allowing you to go back and edit from there.
Pry is a sandbox mode, that allows you to test on the spot different aspects of your code.
Both will help you solve the problems without the need to ask for help.

having said that, here’s faster and more effective way to solve the problem it looks like you are facing.

def longest_str(str)
    hash =
    str.each_char {|ele| hash[ele] += 1}
    sort = hash.sort_by {|k,v| v}

The reason why this works is because we can just use a count for every element of string, counting every element of the array as such

  str.each_char {|ele| hash[ele] += 1}

I recommend you using the values of current (or your string characters) as keys, and the current.length (or your character count) as a value. See if that helps you, as well.