Giter Club home page Giter Club logo

danieldotwav / remove-duplicates-from-sorted-array-i-and-ii Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 0.0 5 KB

This Java program efficiently removes duplicate elements from a sorted array in-place, ensuring the original order of elements is maintained. It's designed to optimize space and time complexity while handling various array scenarios, including empty arrays and arrays with consecutive or non-consecutive duplicates.

Java 100.00%
algorithms arrays code-optimization complexity-analysis duplicate-removal java two-pointer in-place-operations

remove-duplicates-from-sorted-array-i-and-ii's Introduction

Introduction

This Java project implements an efficient algorithm to remove duplicate numbers from a sorted array in-place, preserving the order of the elements. The key challenge addressed is modifying the array so that the first k elements contain the unique elements as they originally appeared in the array.

The second method removeDuplicatesII() removes duplicates from a sorted array while allowing a specified number of duplicates for each unique element.

Algorithms

1. Remove Duplicates I

Logic

  • The algorithm uses a two-pointer approach: one pointer (uniqueNumPtr) to track the position where the next unique element should be placed, and the other to iterate through the array.
  • As it finds a unique element (different from the previous one), it places it at the uniqueNumPtr position.
  • This approach ensures in-place modification of the array with minimal space usage.

Complexity Analysis

  • Time Complexity: O(n) - as the array is iterated through only once.
  • Space Complexity: O(1) - as no additional space is used apart from the input array.

Code Snippet

public class Main {
    public static void main(String[] args) {
        // Test cases with expected outputs
        System.out.println(removeDuplicatesI(new int[] {1, 2, 3})); // Expected Output: 3
        System.out.println(removeDuplicatesI(new int[] {1, 2, 2})); // Expected Output: 2
        // ... other test cases ...
    }

    static int removeDuplicatesI(int[] nums) {
        if (nums.length == 0) return 0;
        int currentUniqueNum = nums[0];
        int uniqueNumPtr = 0;
        for (int i = 1; i < nums.length; ++i) {
            if (nums[i] != currentUniqueNum) {
                currentUniqueNum = nums[i];
                nums[++uniqueNumPtr] = currentUniqueNum;
            }
        }
        return uniqueNumPtr + 1;
    }
}

Test Cases

The following test cases are designed to validate the functionality of the removeDuplicatesI method. Each test case includes the input array and its expected output after removing duplicates.

// Test case 1: No duplicates
System.out.println(removeDuplicatesI(new int[] {1, 2, 3})); // Expected Output: 3

// Test case 2: Consecutive duplicates
System.out.println(removeDuplicatesI(new int[] {1, 2, 2})); // Expected Output: 2

// Test case 3: Non-consecutive duplicates
System.out.println(removeDuplicatesI(new int[] {1, 2, 2, 3, 3, 4})); // Expected Output: 4

// Test case 4: All elements are duplicates
System.out.println(removeDuplicatesI(new int[] {2, 2, 2, 2})); // Expected Output: 1

// Test case 5: Single element
System.out.println(removeDuplicatesI(new int[] {1})); // Expected Output: 1

// Test case 6: Empty array
System.out.println(removeDuplicatesI(new int[] {})); // Expected Output: 0

// Test case 7: Longer array with mixed duplicates
System.out.println(removeDuplicatesI(new int[] {1, 1, 2, 3, 3, 3, 4, 5, 5, 6})); // Expected Output: 6

// Test case 8: Array with all elements being the same
System.out.println(removeDuplicatesI(new int[] {7, 7, 7, 7, 7})); // Expected Output: 1

2. Remove Duplicates II

Logic

  • Processes a sorted array to remove duplicates.
  • Maintains a counter for duplicates.
  • Shifts unique elements to the beginning of the array.

Complexity Analysis

  • Time Complexity: O(n) - Processes each element once.
  • Space Complexity: O(1) - In-place operation with constant space.

Code Snippet

static int removeDuplicatesII(int[] nums) {
    // Handle empty arrays
    int arrSize = nums.length;
    if (arrSize == 0) { return 0; }

    // Adjust the desired frequency of allowed duplicates
    int LIMIT = 2;

    // Store the first number
    int currentUniqueNum = nums[0];
    int uniqueNumPtr = 0;
    int duplicateCounter = 1;

    for (int i = 1; i < arrSize; ++i) {
        if (nums[i] != currentUniqueNum) {
            duplicateCounter = 1;
            currentUniqueNum = nums[i];
            nums[++uniqueNumPtr] = currentUniqueNum;
        }
        else {
            if (duplicateCounter < LIMIT) {
                currentUniqueNum = nums[i];
                nums[++uniqueNumPtr] = currentUniqueNum;
            }
            ++duplicateCounter;
        }
    }
    return uniqueNumPtr + 1;
}

Test Cases

The following test cases are designed to validate the functionality of the removeDuplicatesI method. Each test case includes the input array and its expected output after removing duplicates.

// Test case 1: Non-consecutive duplicates
System.out.println(removeDuplicatesII(new int[] {1, 2, 2, 3, 3, 4})); // Expected Output: 6

// Test case 2: Consecutive duplicates
System.out.println(removeDuplicatesII(new int[] {1, 1, 2, 2, 2, 3, 3, 3, 4})); // Expected Output: 7

// Test case 3: No duplicates
System.out.println(removeDuplicatesII(new int[] {1, 2, 3, 4, 5})); // Expected Output: 5

// Test case 4: Empty array
System.out.println(removeDuplicatesII(new int[] {})); // Expected Output: 0

// Test case 5: All elements are the same
System.out.println(removeDuplicatesII(new int[] {2, 2, 2, 2, 2})); // Expected Output: 2

// Test case 6: Single element array
System.out.println(removeDuplicatesII(new int[] {7})); // Expected Output: 1

remove-duplicates-from-sorted-array-i-and-ii's People

Contributors

danieldotwav avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.