Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am fredysierra on github.
  • I am fysus (https://keybase.io/fysus) on keybase.
  • I have a public key ASA4ZBobKr2LoVRPlhwLnFOgBehVIIEAXwax0VrIV-nY7Ao

To claim this, I am signing this object:

@fredysierra
fredysierra / scala_max_num_array
Last active September 30, 2016 14:01
Given an integer array, find the most frequent number and it's count in the array. Write the code in O(1) space. Eg 1 , 3, 4, 5, 2, 2, 3, 2 Output Most frequent number is 2. The frequency is 3. Return the output as string in 'number: frequency' format. e.g. 2: 3 (Please note the space after : and frequency. If multiple numbers have the same high…
val array = List(1, 3, 4, 5, 2, 2, 3, 2, 5, 5, 5)
val sarray = array.sorted
val (count, maxcount, number, thenumber) = sarray.foldLeft((0, 0, 0, 0)) { (acc, v) =>
{
val (count, maxcount, number, thenumber) = acc
if (number == v) {
(count + 1, maxcount, number, thenumber)
} else {
if (count >= maxcount) {
if (count == maxcount)