Does this work for you?
for(var v in store) {
numberWeAreLookingAt = store[v];
// we hadn't gone through this number yet, so it's not defined
// as a property in the object "frequency"
if(frequency[numberWeAreLookingAt] === undefined)
frequency[numberWeAreLookingAt] = 0; // let's initialize that property with the
// number zero, in it we will hold the
// number of times it appeared
// Sum 1 to the times it appeared already (or zero if we
// initialized it on the "if" above)
frequency[numberWeAreLookingAt] = frequency[numberWeAreLookingAt] + 1;
// the times this number appeared is more than "max"?
if(frequency[numberWeAreLookingAt] > max) {
// then now "max" is the times this number appeared
max = frequency[numberWeAreLookingAt];
// and the result is this number
result = numberWeAreLookingAt;
}
}
Note that the code in your question is perfectly readable. If you really can't read it, you can't "blame it on the programmer": you just don't understand code very well and should work on that.
"Making code readable" doesn't mean making it verbose... it means "make it obvious on a first read to whoever knows the syntax", and I think the code in your question fits that properly. My code is extremely verbose and my comments should be unneeded: they are just explaining what the next line of code does
The only line of code that "may" need explanation is:
frequency[store[v]]=(frequency[store[v]] || 0)+1;
And you can see it's decomposition above... x = (x || 0)+1
means get x if it's defined, or 0 if it's not: then add 1 and assign back to x
, which is what I did on verbose form in my code.
Other thing that seems to confuse the OP (as noted in the comments), is using the brackets syntax to access an object properties. This is not uncommon in dynamically executed languages (and I'd argue that, taking how object prototypes are done in javascript, the brackets syntax makes more sense than the dot syntax, but that's just an oppinion).
In Javascript, you can access object properties with two different syntaxes: object.property
is equivalent to object[property]
. The main difference is that when using the brackets syntax you can use expression to evaluate the property name (or use other variables, as we are doing here). In C#, for example, you could do the same using dynamic
and ExpandoObject
.
Note that while this may confuse you, it's not really that important for the question... you could also think of frequency
being an array where the indexers are objects, instead of a sequential number, and it'd work the same (in PHP, for example, you could achieve this using keyed arrays, or a Dictionary
in C#, and it's a very typical pattern in functional languages).