Ad
Code
Diff
  • public static class Kata 
    {
      public static int SameCase(char a, char b)
      {
        if (!char.IsLetter(a) || !char.IsLetter(b))
        {
          return -1;
        }
    
        return char.IsUpper(a) == char.IsUpper(b) ? 1 : 0;
      }
    }
    // 3ms faster :)
    • public static class Kata
    • {
    • public static int SameCase(char a, char b) =>
    • char.IsLetter(a) && char.IsLetter(b)
    • ? char.IsUpper(a) == char.IsUpper(b)
    • ? 1 : 0
    • : -1;
    • }
    • public static int SameCase(char a, char b)
    • {
    • if (!char.IsLetter(a) || !char.IsLetter(b))
    • {
    • return -1;
    • }
    • return char.IsUpper(a) == char.IsUpper(b) ? 1 : 0;
    • }
    • }
    • // 3ms faster :)

You are given a function called FindSquaresInArray written in C#. This function takes an array of integers and returns the sum of the squares of each element. Your task is to refactor this function to improve its performance and readability.

Constraints:

Do not change the function signature.
Do not use any external libraries.

using System;

public class Program
{
  public static int FindSquaresInArray(int[] arr)
  {
    int sum = 0;
    for (int i = 0; i < arr.Length; i++)
    {
      for (int j = 0; j < arr[i]; j++)
      {
        sum += arr[i];
      }
    }
    return sum;
  }
}