<commit_before>import scala.collection.JavaConversions._

// you can write to stdout for debugging purposes, e.g.
// println("this is a debug message")

object Solution {
  object MajorityElement {

    case class Candidate(idx: Int, count: Int)

    def solve(arr: Array[Int]): Option[Candidate] = {
      val candidate = findCandidate(arr)

      if (isMajority(arr, candidate)) Option(candidate)
      else None
    }

    def isMajority(arr: Array[Int], candidate: Candidate): Boolean = {
      val count = arr.count(_ == arr(candidate.idx))

      if (count > arr.size / 2) true
      else false
    }

    def findCandidate(arr: Array[Int]): Candidate =
      arr.indices.foldLeft(Candidate(0, 1)) { case (acc, idx) =>
        val newAcc =
          if (arr(acc.idx) == arr(idx)) acc.copy(count = acc.count + 1)
          else acc.copy(count = acc.count - 1)

        if (newAcc.count == 0) Candidate(idx, 1)
        else newAcc
      }
  }<commit_msg>Refactoring<commit_after>import scala.collection.JavaConversions._

// you can write to stdout for debugging purposes, e.g.
// println("this is a debug message")

object Solution {
  object MajorityElement {

    case class Candidate(idx: Int, count: Int)

    def solve(arr: Array[Int]): Option[Candidate] = {
      val candidate = findCandidate(arr)

      if (isMajority(arr, candidate)) Option(candidate)
      else None
    }

    def isMajority(arr: Array[Int], candidate: Candidate): Boolean =
      arr.count(_ == arr(candidate.idx)) / 2

    def findCandidate(arr: Array[Int]): Candidate =
      arr.indices.foldLeft(Candidate(0, 1)) { (acc, idx) =>
        val newAcc =
          if (arr(acc.idx) == arr(idx)) acc.copy(count = acc.count + 1)
          else acc.copy(count = acc.count - 1)

        if (newAcc.count == 0) Candidate(idx, 1)
        else newAcc
      }
  }
{'fraction_non_alphanumeric': 0.12894450489662676, 'fraction_numerical': 0.007616974972796518, 'mean_word_length': 2.7378048780487805, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6626950', 'n_tokens_mistral': 609, 'n_tokens_neox': 578, 'n_words': 159}