C# LINQ doesn't work, very wired!


  • -1
    public class Solution {
        public int RemoveElement(int[] nums, int val) {
            nums=nums.Where(m=>m!=val).ToArray();
            return nums.Length;
        }
    }
    

    Leetcode Submission Result: Wrong Answer
    Input: [4,5]
    4
    Output: [4]
    Expected: [5]

    But I tested in local.
    it works as expected [5].

    Wired


  • 2

    I'm not a C# expert, but I think this statement allocates a new array and assign to the nums reference:

    nums.Where(m=>m!=val).ToArray()
    

    Therefore from the caller side it has no idea what's being changed in the function.


  • 0

    You might be correct. But I runs well on my local machine. It should be something related leetcode c# simulator.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.