Personally, I chose to sort because I could easily pull the rest of the algorithm right out of my head without looking anything up. It certainly isn't necessary but it got me to working code much faster.How is sorting helping to solve the problem? I don't see it as being necessary.
Here's an even more compact solution in Swift, but I had to look up the "filter" and "count" function.
I like this one because it's very close to how a human would approach it.
Code:
var arr = [ 2, 3, 9, 4, 2, 9, 5, 1, 1, 3, 1, -2, 5, 1]
var x = 0
var xMax = 0
for i in arr {
let x = arr.filter{$0 == i}.count
if x > xMax {
candidate = i
xMax = x
}
}
print(candidate) // 1
print(xMax) // 4