© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
24 replies
aa battery

❔ Just got started with leetcode after a year of learning, I need some help...

So the title says it all I guess, I started with leetcode after around a year with learning and I started away with one of its problems. It took me two hours to finally get it right but my code felt so redundant and long for such a simple problem. Can I get some feedback on how to make it more efficient? Thanks

So here is what I had after I was done (it was the two sum problem)
public class Solution {
    public int[] TwoSum(int[] nums, int target) {
        for(int i = 0; i <= nums.Length; i++) {
            int e = i < nums.Length ? i + 1 : i;
            var excludedNums= new int[nums.Length-e];
            Array.Copy(nums, e, excludedNums, 0, excludedNums.Length);

            foreach(int a in excludedNums) {
                int test = nums[i] + a;
                if (test == target) {
                    if (a == nums[i]) {
                        return new int[] { i, Array.IndexOf(nums, a, i+1) };
                    }
                    return new int[] { i, Array.IndexOf(nums, a) };
                };
            }
        }
        return new int[2];
    }
}
public class Solution {
    public int[] TwoSum(int[] nums, int target) {
        for(int i = 0; i <= nums.Length; i++) {
            int e = i < nums.Length ? i + 1 : i;
            var excludedNums= new int[nums.Length-e];
            Array.Copy(nums, e, excludedNums, 0, excludedNums.Length);

            foreach(int a in excludedNums) {
                int test = nums[i] + a;
                if (test == target) {
                    if (a == nums[i]) {
                        return new int[] { i, Array.IndexOf(nums, a, i+1) };
                    }
                    return new int[] { i, Array.IndexOf(nums, a) };
                };
            }
        }
        return new int[2];
    }
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Just started learning csharp and need help
C#CC# / help
4y ago
I just started
C#CC# / help
8mo ago
I need some help with attribute
C#CC# / help
3y ago