Nov 20, 2022

Solution 1 :

Just most[0] would have fixed your code.

Changed to just track maximum count.

``````let As = document.getElementsByTagName('a');

let towns = new Map();
let max = [];

for(let a of As)
{
let town = a.textContent.split(',')[1].trim()
if(towns.has(town))
{
const count = towns.get(town)+1;
towns.set(town, count);
max[count] = town;
}
else
{
towns.set(town, 1);
max[1] = town;
}
}

let most = max.pop()

console.log(most);``````
``````<a href="/search/details/50/1/" class="address">9207 Odin Wy, Bothell</a>
<a href="/search/details/50/1/" class="address">9207 Odin Wy, Bellevue</a>
<a href="/search/details/50/1/" class="address">9207 Odin Wy, Kirkland</a>
<a href="/search/details/50/1/" class="address">9207 Odin Wy, Monroe</a>
<a href="/search/details/50/1/" class="address">9207 Odin Wy, Bothell</a>
<a href="/search/details/50/1/" class="address">9207 Odin Wy, Bothell</a>
<a href="/search/details/50/1/" class="address">9207 Odin Wy, Bothell</a>``````

Solution 2 :

`[...towns.entries()]` is an Array of Array(2). Just pick the first element of the first element of the sorted. You don’t need the `entries`-part by the way, just spread the `Map` to an `Array`.

Alternatively you can use a reducer to create a frequency table, included that in te snippet.

``````let towns = new Map();

document.querySelectorAll('a').forEach(href => {
const town = href.textContent.split(',')[1].trim();
if(towns.has(town)) {
towns.set(town, towns.get(town)+1)
} else {
towns.set(town, 1);
}
});

const most = [...towns].sort((a, b) => b[1] - a[1])[0][0];
//                                                    ^ here
console.log(most);

// alternative use reduce to create a frequency table
const freqs = [...document.querySelectorAll('a')]
.reduce( (acc, val) => {
const town = val.textContent.split(',')[1].trim();
return {...acc, [town]: acc[town] ? acc[town] + 1 : 1 };
}, {});
// now you need entries ;)
console.log(Object.entries(freqs).sort( (a, b) => b[1] - a[1] )[0][0]);``````
``a {display: block}``
``````<a href="/search/details/50/1/" class="address">9207 Odin Wy, Bothell</a>
<a href="/search/details/50/1/" class="address">9207 Odin Wy, Bellevue</a>
<a href="/search/details/50/1/" class="address">9207 Odin Wy, Kirkland</a>
<a href="/search/details/50/1/" class="address">9207 Odin Wy, Monroe</a>
<a href="/search/details/50/1/" class="address">9207 Odin Wy, Bothell</a>
<a href="/search/details/50/1/" class="address">9207 Odin Wy, Bothell</a>
<a href="/search/details/50/1/" class="address">9207 Odin Wy, Bothell</a>``````

Problem :

Here is my code. The output equals “Bothell,4”. What can I do to make the output “Bothell” without the comma and count portion?

``````let As = document.getElementsByTagName('a');

let towns = new Map();

for(let a of As)
{
let town = a.textContent.split(',')[1].trim()
if(towns.has(town))
{
towns.set(town, towns.get(town)+1)
}
else
{
towns.set(town, 1);
}
}

let most = [...towns.entries()].sort((a, b) => b[1] - a[1])[0]

console.log(most);``````
``````<a href="/search/details/50/1/" class="address">9207 Odin Wy, Bothell</a>
<a href="/search/details/50/1/" class="address">9207 Odin Wy, Bellevue</a>
<a href="/search/details/50/1/" class="address">9207 Odin Wy, Kirkland</a>
<a href="/search/details/50/1/" class="address">9207 Odin Wy, Monroe</a>
<a href="/search/details/50/1/" class="address">9207 Odin Wy, Bothell</a>
<a href="/search/details/50/1/" class="address">9207 Odin Wy, Bothell</a>
<a href="/search/details/50/1/" class="address">9207 Odin Wy, Bothell</a>``````

Comment posted by user120242

do you mean most[0]?

Comment posted by Kevin Boroumand

Wow… it was literally just adding another[0]. Thanks so much!!

Comment posted by Kevin Boroumand

keys works as well. Thanks all!

Comment posted by KooiInc

@user120242 You are sorting on keys. Bothell being the first is a coincidence. I think OP needs the most frequent town in the collection.

@KevinBoroumand This is completely wrong. This is just subtracting the second letter of each word in the compareFunction like