Hey Sup Forums i'm trying to learn to optimize a hash array on problem "Display the first repeating character on a word." char hashArray (string key) { int arr[key.length()]; memset(arr,0,sizeof arr); for(int x = 0; x < key.length(); x++) { arr[(int)key[x]%key.length()] += 1; if(arr[(int)key[x]%key.length()] == 2){ return key[x]; } } return NULL; }
Which O(n) but this one char loopArray (string key) { for(int x = 0; x < key.length(); x++) { for (int y = x+1; y < key.length(); y++) { if (key[x] == key[y]) { return key[x]; } } } return NULL;
which is O(n^2) performs better. any suggestions except not using c++?
Why the fuck do you even need a hash? Just iterate through the array and compare the current character to the previous character. If they are the same, then that means it's the first repeating.
Worst case will be O(n) since it'll go through the entire array.
Fucking retards.
Ayden Morales
This desu. OP complicating it for no reason. I wouldn't hire his ass just for making things complicated for no reason.
Owen Fisher
Define "repeating character".
Jaxon Green
Pizza.
First repeating is a z.
Angel Edwards
The hash code is actually O(1) not O(n).
Colton Peterson
> returning null
Go fuck yourself.
Owen Brown
Ambiguous.
Tyler Foster
What would you prefer it return if there is no repeating character?
James Sanchez
OP may be retarded but with the hash you can get constant time
Kevin Hughes
How the fuck is it O(1) when it's literally iterating the length of the entire array?
Jesus fucking christ, get off my board you fucking cumguzzler.
Caleb Peterson
abba. Does a count as a repeating letter? Is it first?
Jace Scott
Negative number since no ascii characters are negative. 0 is.
This allows for proper checking.
Adam Diaz
Good question. OP is a retard for not posting specifics. If we go with the pizza example, then the best solution is clearly else we need a different approach.
An Option.
Anthony Miller
what do you do for azza?
Jack Peterson
>An Option. Those do not exist in C
Thomas Sanders
>Does a count as a repeating letter? Yes >Is it first? No
>C++ >properly working with Unicode It won't even compute the length of the string correctly, because utf-8 characters are represented by variable number of bytes. You'll need a proper language to do that hassle-free for Unicode.
Logan Walker
Rust does it. And you can avoid nulls too.
Nathan Cooper
>IntMaybe * result = intMaybeBind(intMaybeBind(intMaybeBind(intMaybeBind(intMaybeReturn(i), specialInc), specialInc), specialInc), specialInc);