Remove Element - LeetCode - 27 - Java Solution

Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The order of the elements may be changed

Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The order of the elements may be changed. Then return the number of elements in nums which are not equal to val.

Solution : 

[

class Solution {

    public int removeElement(int[] nums, int val) {

        int counter = 0;

        for (int i = 0; i < nums.length; i++){

            if (nums[i] != val){

                nums[counter] = nums[i];

                counter++;

            }

        }


        return counter;

    }

}

]

Explanation :  

All you have to do is take elements not equal to K and put them before the k elements in an array. So we make a counter start at 0. 

Now we run a for loop over the given array. Whenever you find an element that is not equal to k you put that element into the position of the array[counter]. 

All the elements which are not equal to k will be at the start of the array. Now return counter. 

Time Complexity : O (N)

Space Complexity : O (1)

Let me know in the comments what you think about this problem. 

COMMENTS

BLOGGER
Name

Affiliate marketing tutorial,2,Android,60,Android Development,22,Android tutorials,41,Blogging tactics,8,Deals,1,DSA,4,Elite Youtube Tutorial,4,Firebase,1,Flutter,4,Fuchsia,1,Hosting Guides,3,Leetcode,4,Miscellaneous,10,Mobile Gaming,17,NTN,94,Problogging,7,PUBG,16,Reviews,1,SEO Tutorial,5,Start a blog,9,Start a channel,6,Top 5,4,Travel Blogging,4,Windows,12,Windows Security,4,Windows tutorials,7,Wordpress Tutorials,4,Youtube Tutorials,11,
ltr
item
A Passionate Developers Creation: Remove Element - LeetCode - 27 - Java Solution
Remove Element - LeetCode - 27 - Java Solution
Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The order of the elements may be changed
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg5WJTAyxmEFJBsnizYcG4J219FEfCJmJjDPCc5w210RovPs6SIFEhv6DIryPNG4ANNDWbXCHY9rqXVNP5uO4PdxoPwyTAvgMX2C30jkSDS_Un6hQqva8yeTtRdiD1vNLXBz79LhKO_w6DLKr_Y5T1pScKgFwgnD22eXh0BszF1jds4MQAp6Sh3RkHhpv0/w200-h133/christopher-gower-m_HRfLhgABo-unsplash.jpg
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg5WJTAyxmEFJBsnizYcG4J219FEfCJmJjDPCc5w210RovPs6SIFEhv6DIryPNG4ANNDWbXCHY9rqXVNP5uO4PdxoPwyTAvgMX2C30jkSDS_Un6hQqva8yeTtRdiD1vNLXBz79LhKO_w6DLKr_Y5T1pScKgFwgnD22eXh0BszF1jds4MQAp6Sh3RkHhpv0/s72-w200-c-h133/christopher-gower-m_HRfLhgABo-unsplash.jpg
A Passionate Developers Creation
https://www.apdevc.com/2024/10/remove-element-leetcode-27-java-solution.html
https://www.apdevc.com/
https://www.apdevc.com/
https://www.apdevc.com/2024/10/remove-element-leetcode-27-java-solution.html
true
3690156770086819421
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content