Most Clean Solution in 13 Languages | O(1) Space
Intuition\n Describe your first thoughts on how to solve this problem. \nAfter every iteration, the element which is largest till now would be in the first plac
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nAfter every iteration, the element which is largest till now would be in the first place for comparison.\n`what would happen if the first element is largest ?`\nthen for every iteration our winner would be the first element, because it is largest.\nExample: `[4,3,2,1]`\n[4,3,2,1] -> [4,2,1,3] -> [4,1,3,2] -> [4,3,2,1] -> ..... ( ans so on )\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Initialize max element as first element. `max = arr[0]`\n- now start from `i = 1 to n-1` \n- compare current element with the max, if max wins increase the count, if the curr element wins then update max as this element and assign `count = 1`\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    public int getWinner(int[] arr, int k) {\n        int max = arr[0];\n        int count = 0;\n        for(int i=1;i<arr.length&&count<k;i++) {\n            if(max < arr[i]) {\n                max = arr[i];\n                count = 0;\n            }\n            count++;\n        }\n        return max;\n    }\n}\n```\n```C++ []\nclass Solution {\npublic:\n    int getWinner(vector<int>& arr, int k) {\n        std::ios::sync_with_stdio(false);\n        cin.tie(nullptr);\n        cout.tie(nullptr);\n\n        int max_val = arr[0];\n        int count = 0;\n        for (int i = 1; i < arr.size() && count < k; ++i) {\n            if (max_val < arr[i]) {\n                max_val = arr[i];\n                count = 0;\n            }\n            ++count;\n        }\n        return max_val;\n    }\n};\n```\n```Python []\nclass Solution:\n    def getWinner(self, arr: List[int], k: int) -> int:\n        max_val, count = arr[0], 0\n        for i in range(1, len(arr)):\n            if max_val < arr[i]:\n                max_val = arr[i]\n                count = 0\n            count += 1\n            if count == k:\n                break\n        return max_val\n```\n```C# []\npublic class Solution {\n    public int GetWinner(int[] arr, int k) {\n        int maxVal = arr[0];\n        int count = 0;\n        for (int i = 1; i < arr.Length && count < k; i++) {\n            if (maxVal < arr[i]) {\n                maxVal = arr[i];\n                count = 0;\n            }\n            count++;\n        }\n        return maxVal;\n    }\n}\n```\n```Scala []\nobject Solution {\n    def getWinner(arr: Array[Int], k: Int): Int = {\n        var maxVal = arr(0)\n        var count = 0\n        for (i <- 1 until arr.length) {\n            if (maxVal < arr(i)) {\n                maxVal = arr(i)\n                count = 0\n            }\n            count += 1\n            if (count == k) {\n                return maxVal\n            }\n        }\n        maxVal\n    }\n}\n```\n```Javascript []\nvar getWinner = function(arr, k) {\n    let maxVal = arr[0];\n    let count = 0;\n    for (let i = 1; i < arr.length && count < k; ++i) {\n        if (maxVal < arr[i]) {\n            maxVal = arr[i];\n            count = 0;\n        }\n        ++count;\n    }\n    return maxVal;\n};\n```\n```Kotlin []\nclass Solution {\n    fun getWinner(arr: IntArray, k: Int): Int {\n        var maxVal = arr[0]\n        var count = 0\n        for (i in 1 until arr.size) {\n            if (maxVal < arr[i]) {\n                maxVal = arr[i]\n                count = 0\n            }\n            count++\n            if (count == k) {\n                break\n            }\n        }\n        return maxVal\n    }\n}\n```\n```Go []\nfunc getWinner(arr []int, k int) int {\n    maxVal, count := arr[0], 0\n    for i := 1; i < len(arr); i++ {\n        if maxVal < arr[i] {\n            maxVal = arr[i]\n            count = 0\n        }\n        count++\n        if count == k {\n            return maxVal\n        }\n    }\n    return maxVal\n}\n```\n```PHP []\nclass Solution {\n\n    /**\n     * @param Integer[] $arr\n     * @param Integer $k\n     * @return Integer\n     */\n    function getWinner($arr, $k) {\n        $max = $arr[0];\n        $count = 0;\n        for ($i = 1; $i < count($arr) && $count < $k; $i++) {\n            if ($max < $arr[$i]) {\n                $max = $arr[$i];\n                $count = 0;\n            }\n            $count++;\n        }\n        return $max;\n    }\n}\n```\n```Ruby []\n# @param {Integer[]} arr\n# @param {Integer} k\n# @return {Integer}\ndef get_winner(arr, k)\n    max_val, count = arr[0], 0\n    (1...arr.length).each do |i|\n        if max_val < arr[i]\n            max_val = arr[i]\n            count = 0\n        end\n        count += 1\n        return max_val if count == k\n    end\n    max_val\nend\n```\n```Rust []\nimpl Solution {\n    pub fn get_winner(arr: Vec<i32>, k: i32) -> i32 {\n        let mut max_val = arr[0];\n        let mut count = 0;\n        for i in 1..arr.len() {\n            if max_val < arr[i] {\n                max_val = arr[i];\n                count = 0;\n            }\n            count += 1;\n            if count == k {\n                return max_val;\n            }\n        }\n        max_val\n    }\n}\n```\n```Swift []\nclass Solution {\n    func getWinner(_ arr: [Int], _ k: Int) -> Int {\n        var max = arr[0]\n        var count = 0\n        for i in 1..<arr.count where count < k {\n            if max < arr[i] {\n                max = arr[i]\n                count = 0\n            }\n            count += 1\n        }\n        return max\n    }\n}\n```\n```Dart []\nclass Solution {\n  int getWinner(List<int> arr, int k) {\n    int max = arr[0];\n    int count = 0;\n    for (int i = 1; i < arr.length && count < k; i++) {\n        if (max < arr[i]) {\n            max = arr[i];\n            count = 0;\n        }\n        count++;\n    }\n    return max;\n  }\n}\n```\n\nThanks\nIf you like this , Please upvote to learn all languages
['PHP', 'C++', 'Java', 'Go', 'Python3', 'Scala', 'Ruby', 'Kotlin', 'JavaScript', 'C#']