AdSense

Tuesday, January 6, 2015

Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice?
For example,
Given sorted array A = [1,1,1,2,2,3],
Your function should return length = 5, and A is now [1,1,2,2,3].
This is not a hard problem. However, it is tricky to write neat code. A smart way to do it is to track two variables, the current node (curr) and the node before it (prev). If current node equals the previous node and the node before the previous node, we traverse all duplicate nodes and skip them until we find the next different one, mark the index of that node.
Then we move the prev to the next one, and let num[prev] = num[curr]. Then we increase curr.

For example, num = {1, 1, 1, 2, 2, 3, 3, 3};
let prev = 1 and curr = 2, because it doesn't matter if num[0] == num[1] or not.
num[cur] == num[prev] && num[curr] == num[prev - 1], curr++ -> curr = 2
now num[curr] = 2, which doesn't equal to num[1] and num[0], we increase prev to 2 and let num[2] = 2. And we increment curr. The whole procedure would be like this:





public int removeDuplicates(int[] A) {
        if (A == null)
            throw new NullPointerException("Null Array!");
        if (A.length <= 2)
            return A.length;
        
        int prev = 1;
        int curr = 2;
        while (curr < A.length) {
            if (A[curr] == A[prev] && A[curr] == A[prev - 1]) {
                curr++;
            }
            else {
                prev++;
                A[prev] = A[curr];
                curr++;
            }
        
        }
        return prev + 1;
    }

1 comment:

  1. The development of artificial intelligence (AI) has propelled more programming architects, information scientists, and different experts to investigate the plausibility of a vocation in machine learning. Notwithstanding, a few newcomers will in general spotlight a lot on hypothesis and insufficient on commonsense application. IEEE final year projects on machine learning In case you will succeed, you have to begin building machine learning projects in the near future.

    Projects assist you with improving your applied ML skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include projects into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Final Year Project Centers in Chennai even arrange a more significant compensation.


    Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.

    ReplyDelete