Python/JS/Java/Go/C++ O(n) by iteration [w/ Comment]
O(n) by iteration\n\n\u4E2D\u6587\u8A73\u89E3 \u89E3\u984C\u6587\u7AE0\n\nTutorial video in Chinese \u4E2D\u6587\u89E3\u984C\u5F71\u7247\n\n\n\n---\n\nImplement
O(n) by iteration\n\n[\u4E2D\u6587\u8A73\u89E3 \u89E3\u984C\u6587\u7AE0](https://vocus.cc/article/6559d622fd89780001a1f96c)\n\n[Tutorial video in Chinese \u4E2D\u6587\u89E3\u984C\u5F71\u7247](https://youtu.be/tTfkdaB4_hM)\n\n![image](https://assets.leetcode.com/users/images/967ea20c-7acc-48ac-a4d5-60c8aa17316e_1615731413.5755115.png)\n\n---\n\n**Implementation**:\n\n**Python**:\n\n```\nclass Solution:\n    def diagonalSum(self, mat: List[List[int]]) -> int:\n        \n        n = len(mat)\n        \n        mid = n // 2\n        \n        summation = 0\n        \n        for i in range(n):\n            \n            # primary diagonal\n            summation += mat[i][i]\n            \n            # secondary diagonal\n            summation += mat[n-1-i][i]\n            \n            \n        if n % 2 == 1:\n            # remove center element (repeated) on odd side-length case\n            summation -= mat[mid][mid]\n            \n            \n        return summation\n```\n\n---\n\n**Java**:\n\n```\nclass Solution {\n    public int diagonalSum(int[][] mat) {\n        \n        // side length\n        int n = mat.length;\n        \n        // mid point index\n        int mid = n / 2;\n        \n        // summation of diagonal element\n        int summation = 0;\n        \n        for( int i = 0 ; i < n ; i++ ){\n            \n            // primary diagonal\n            summation += mat[i][i];\n            \n            // secondary diagonal\n            summation += mat[n-1-i][i];\n        }\n        \n        \n        if( n % 2 == 1 ){\n            \n            // remove center element (repeated) on odd side-length case\n            summation -= mat[mid][mid];\n        }\n        \n        return summation;\n    }\n}\n```\n\n---\n\n**Javascript**:\n\n```\nvar diagonalSum = function(mat) {\n    \n    // side length\n    let n = mat.length;\n    \n    // mid point index\n    let mid = Math.floor( n / 2 );\n    \n    // summation of diagonal element\n    let summation = 0;\n    \n    for( let i = 0 ; i < n ; i++ ){\n        \n        // primary diagonal\n        summation += mat[i][i];\n        \n        // secondary diagonal\n        summation += mat[n-1-i][i];\n    }\n    \n    if( n % 2 == 1 ){\n        \n        // remoive center element (repeated) on odd side-length case\n        summation -= mat[mid][mid];\n    }\n    \n    return summation;\n};\n```\n\n---\n\n**Golang**:\n\n```\nfunc diagonalSum(mat [][]int) int {\n    \n    // side-length\n    n := len(mat)\n    \n    // mid point index\n    mid := n / 2\n    \n    // summation of diagonal element\n    summation := 0\n    \n    for i := 0 ; i < n ; i++{\n        \n        // primary diagonal\n        summation += mat[i][i]\n        \n        // secondary diagonal\n        summation += mat[n-1-i][i]\n        \n    }\n    \n    if n % 2 == 1 {\n        \n        // remove center element (repeated) on odd side-length case\n        summation -= mat[mid][mid]\n    }\n    \n    \n    return summation\n}\n```\n\n---\n\n**C++**:\n\n```\nclass Solution {\npublic:\n    int diagonalSum(vector<vector<int>>& mat) {\n        \n        // side length\n        int n = mat.size();\n        \n        // mid point index\n        int mid = n / 2;\n        \n        // summation of diagonal element\n        int summation = 0;\n        \n        for( int i = 0 ; i < n ; i++ ){\n            \n            // primary diagonal\n            summation += mat[i][i];\n            \n            // secondary diagonal\n            summation += mat[n-1-i][i];\n        }\n        \n        \n        if( n % 2 == 1 ){\n            \n            // remove center element (repeated) on odd side-length case\n            summation -= mat[mid][mid];\n        }\n        \n        return summation;\n    }\n};\n```
['C', 'Iterator', 'Python', 'C++', 'Java', 'Go', 'Python3', 'JavaScript']