/*
Josiah 6:56 PM
I snagged Given a string, return the character that is most
commonly used in the string.
--- Examples
maxChar("abcccccccd") === "c"
maxChar("apple 1231111") === "1"
*/
JavaScript
// find elements
var banner = $("#banner-message")
var button = $("button")
// notes --
/*
Sheldon and Josiah solution was a slow attempt at just
solving the problem and not analyzing the optimal way
mostly focused on getting the language (code) to do it.
After more thought, code isn't the importance the logic
and awareness of the WHY, and how many, Big O type stuff
is necessary.
The following is our quick solution but very heavy cycle and memory use
Is the goal to write it, or to really think it in 30 minutes ?
Note, our result returns any/all that match the highest count compared
to the internet solution.
I've spent more time than should on this though.
*/
function ourFirstAttempt(x) {
$('#result').append("<br><br>Our answer<br><br>");
$('#result').append("<br>param:'" + x + "'<BR><BR>");
// find a place for totals
let values = [];
// set totals to empty
for(var z = 0; z<256; z++) {
values [z] = 0;
}
// for each character in string, find the storage location
// increase value at the storage location
for(i=0; i<x.length; i++) {
let y = x.charAt(i).charCodeAt(0);
values[y]++;
}
// find the value in storage that has the highest count
currentHighestCount = 0 ;
values.forEach(element => {
if (element > currentHighestCount){
currentHighestCount = Math.floor(element);
}
})
// show the characters that are in the positions that have highest counts
for (var i=0;i<256;i++){
if (values[i]>=currentHighestCount) {
$('#result').append("<br>"+("'"+String.fromCharCode(i) + "'"+":"+ currentHighestCount+"<br>"));
}
}
}
/*
The following internet answer uses its version of a dictionary
to hash the totals into spots key'ed by the characters
It makes one run through the array and captures all the
info it needs in that one iteration
*/
function internetAnswer(exp) {
var expCounts = {};
var maxKey = '';
for(var i = 0; i < exp.length; i++)
{
var key =...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.