Hey Sup Forums i'm trying to learn to optimize a hash array on problem "Display the first repeating character on a word...

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++?

Other urls found in this thread:

youtube.com/watch?v=GJdiM-muYqc
cs.utah.edu/~arichard/monad.html
en.cppreference.com/w/cpp/utility/optional
twitter.com/AnonBabble

>Display the first repeating character
Make an array size of 255 and use ASCII values as the index. In general case use hashmap instead of array.

The idea is to simply count the number of letters. If the value in the array is 1, you found your repeating letter.

>If the value in the array is 1, you found your repeating letter.
It's not a repeating letter if it's only 1 letter.

if(array[letter]==1)
return letter;
else
array[letter]++;

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.

This desu. OP complicating it for no reason. I wouldn't hire his ass just for making things complicated for no reason.

Define "repeating character".

Pizza.

First repeating is a z.

The hash code is actually O(1) not O(n).

> returning null

Go fuck yourself.

Ambiguous.

What would you prefer it return if there is no repeating character?

OP may be retarded but with the hash you can get constant time

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.

abba.
Does a count as a repeating letter? Is it first?

Negative number since no ascii characters are negative. 0 is.

This allows for proper checking.

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.

what do you do for azza?

>An Option.
Those do not exist in C

>Does a count as a repeating letter?
Yes
>Is it first?
No

z

throw an exception.

Then

See Faggot.

youtube.com/watch?v=GJdiM-muYqc

> assuming ascii when the world is unicode

Do you know what character encoding will be used? You could get rid of the hash map completely if you know.

throwing an exception is absolutely retarded if you're not going to fix it
just return -1

>In general case use hashmap instead of array.

cs.utah.edu/~arichard/monad.html

>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.

Rust does it. And you can avoid nulls too.

>IntMaybe * result = intMaybeBind(intMaybeBind(intMaybeBind(intMaybeBind(intMaybeReturn(i), specialInc), specialInc), specialInc), specialInc);

Beaituful

They do in sepples.
en.cppreference.com/w/cpp/utility/optional