0

I have a very large array (around 50 000 elements, consisting of single words). I would like to be able to get the most commonly used words, and their frequency in the shortest possible time. The problem with my current method is that:

  • 25 elements takes 0 seconds
  • 250 elements takes 0 seconds
  • 2 500 elements takes 1 second
  • 25 000 elements takes 30 seconds
  • 50 000 elements takes a minute and a half.

It's not hard to see, that this goes up exponentially, and I'm pretty sure that is because of my method. So my question is whether it is possible to do this in a linear amount of time. The result can be any data format, array of objects, or one object,

mplungjan
  • 169,008
  • 28
  • 173
  • 236
matidfk
  • 35
  • 4
  • Please visit the [help], take the [tour] to see what and [ask]. Do some research, search for related topics on SO; if you get stuck, post a [mcve] of your attempt, noting input and expected output using the `[<>]` snippet editor. – mplungjan Jan 20 '21 at 09:01
  • 3
    where is the algorithm *you* are currently using? – Andrea Giammarchi Jan 20 '21 at 09:02
  • @mplungjan [javascript - Get the element with the highest occurrence in an array - Stack Overflow](https://stackoverflow.com/questions/1053843/get-the-element-with-the-highest-occurrence-in-an-array) is more specific in this case. – user202729 Jan 20 '21 at 09:07
  • @user202729 I added it to the list of dupes – mplungjan Jan 20 '21 at 09:24

0 Answers0