Interview Question

Software Engineer - New Grad Interview

-Vancouver, BC

Axiom Zen

Using a language of your preference, write a method that returns the most common character in a string.

AnswerAdd Tags

Interview Answers

2 Answers

0

Answer in javascript function findMostChar(str) { var cal = []; str = str.toLowerCase().replace(/\s+/g, ''); function hasValue(s) { for (var i = 0; i a.count) return 1; if(b.count < a.count) return -1; return 0; }) console.log(cal[0]) } findMostChar("you can sort the keys and then retrieve the associated values");

Anonymous on

0

With Python: from collection import Counter Counter(s).most_common(1)[0][0] With javascript function mostCommonCharacter(s) { var counter = {} var max = 0; var mostCommon; for (var x of s) counter[x] = (counter[x] || 0) + 1; for (var chr in counter) if (counter[chr] >= max) { max = counter[chr]; mostCommon = chr; } return mostCommon; }

Anonymous on

Add Answers or Comments

To comment on this, Sign In or Sign Up.