0

I know I could go through a for loop like this (see code) and i could also add to the array in the same fashion but is there a quicker way. I don't want to use any other java API as i want to practice array's. Would using a hash function allow me to store my variables quicker and then find a certain word quicker?

edit: The problem is when using 10,000+ words the delay increases larger than 1ms

Thanks :)

int count = 0;
for(int i = 0; i < array.length; i++)
    if(array[i].equals(word)) 
        count++;
Pshemo
  • 122,468
  • 25
  • 185
  • 269
Kingsta1993
  • 25
  • 1
  • 1
  • 5

3 Answers3

1

You can use a 2 dimensional array :

Array [Alphabet] [Words starting with that alphabet]

Sarthak Mittal
  • 5,794
  • 2
  • 24
  • 44
  • This is a very simplified version of a hash map. The hash function of using the first letter is not a very good one, but if you can't use a hash map directly, it's not that bad. – Michael Myers Nov 19 '14 at 16:34
  • Adding to a 2d array would increase the time because i would need two for loops right? – Kingsta1993 Nov 19 '14 at 16:36
  • yes you are right, but the guy wanted to use and practice arrays only, so i replied with that answer :) – Sarthak Mittal Nov 19 '14 at 16:37
  • @MichaelMyers how could i create a simplified version of a hash map for this? Could you point me in the right direction please – Kingsta1993 Nov 19 '14 at 16:37
  • @kingsta: no dude you just check the first alphabet of the word and directly access the array starting with those words – Sarthak Mittal Nov 19 '14 at 16:39
  • @Kingsta1993: A hash map first uses some function to calculate a hash value of each item, then stores the item in a "bucket" based on the hash value. That's what this answer suggests: the hash function is simply the first letter of the word, then the bucket is the array corresponding to that letter. – Michael Myers Nov 19 '14 at 16:46
  • let me give you a better insight suppose your array is something like : array[26][] now whenever you encounter a word, fetch its first alphabets ascii value, if it is <=90 add 32 in it and then subtract 97, this will take care of both small and capital alphabets and access the array was i clear? – Sarthak Mittal Nov 19 '14 at 16:47
  • Note that this works best if you disallow non-English letters. If one of your words, is, say, [Åland](https://en.wikipedia.org/wiki/%C3%85land_Islands), you'll have problems. Or you could just make a 27th bucket for such words. – Michael Myers Nov 19 '14 at 16:52
  • @Michael: Yes there would be a lot of cases to consider if we start using non-English letters :) – Sarthak Mittal Nov 20 '14 at 07:05
1

You could pre-sort the array and then use a binary-chop search on it. This would only be useful if you are looking for many words.

If you allow other structures then you can usually achieve O(1) lookup times.

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
0

Whenever your processing 10,000 words you can expect some delays. Depending on what word is set to you might be able to filter a bit better but as far as your code shows that's the best way to do it.

JGerulskis
  • 800
  • 2
  • 10
  • 24