Don't know what happen


  • 0
    0

    this is my code
    public class Solution {
    public int removeElement(int[] nums, int val) {
    int count=0;
    int length=0;

    for(int i=0;i<nums.length;i++){
    	if(val==nums[i]){
    		count++;
    	}
    }
    length=nums.length-count;
    return length;
    }
    

    }

    as the question, it ask for returning the new array's length.
    i use a count to record the duplicate amount. then i use original array length minute count to give new length .

    but why it gives me the wrong answer
    Input:
    [4,5], 4
    Output:
    [4]
    Expected:
    [5]


  • 2

    Well the problem is even called "remove element" and you don't remove anything. You don't even change nums at all.

    Btw, can you please format your code properly? Bit rude to not do that especially when you're asking for help.


Log in to reply
 

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