Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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.

NotificationsYou must be signed in to change notification settings

danieldotwav/Remove-Duplicates-From-Sorted-Array-I-and-II

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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 methodremoveDuplicatesII() 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

publicclassMain {publicstaticvoidmain(String[]args) {// Test cases with expected outputsSystem.out.println(removeDuplicatesI(newint[] {1,2,3}));// Expected Output: 3System.out.println(removeDuplicatesI(newint[] {1,2,2}));// Expected Output: 2// ... other test cases ...    }staticintremoveDuplicatesI(int[]nums) {if (nums.length ==0)return0;intcurrentUniqueNum =nums[0];intuniqueNumPtr =0;for (inti =1;i <nums.length; ++i) {if (nums[i] !=currentUniqueNum) {currentUniqueNum =nums[i];nums[++uniqueNumPtr] =currentUniqueNum;            }        }returnuniqueNumPtr +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 duplicatesSystem.out.println(removeDuplicatesI(newint[] {1,2,3}));// Expected Output: 3// Test case 2: Consecutive duplicatesSystem.out.println(removeDuplicatesI(newint[] {1,2,2}));// Expected Output: 2// Test case 3: Non-consecutive duplicatesSystem.out.println(removeDuplicatesI(newint[] {1,2,2,3,3,4}));// Expected Output: 4// Test case 4: All elements are duplicatesSystem.out.println(removeDuplicatesI(newint[] {2,2,2,2}));// Expected Output: 1// Test case 5: Single elementSystem.out.println(removeDuplicatesI(newint[] {1}));// Expected Output: 1// Test case 6: Empty arraySystem.out.println(removeDuplicatesI(newint[] {}));// Expected Output: 0// Test case 7: Longer array with mixed duplicatesSystem.out.println(removeDuplicatesI(newint[] {1,1,2,3,3,3,4,5,5,6}));// Expected Output: 6// Test case 8: Array with all elements being the sameSystem.out.println(removeDuplicatesI(newint[] {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

staticintremoveDuplicatesII(int[]nums) {// Handle empty arraysintarrSize =nums.length;if (arrSize ==0) {return0; }// Adjust the desired frequency of allowed duplicatesintLIMIT =2;// Store the first numberintcurrentUniqueNum =nums[0];intuniqueNumPtr =0;intduplicateCounter =1;for (inti =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;        }    }returnuniqueNumPtr +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 duplicatesSystem.out.println(removeDuplicatesII(newint[] {1,2,2,3,3,4}));// Expected Output: 6// Test case 2: Consecutive duplicatesSystem.out.println(removeDuplicatesII(newint[] {1,1,2,2,2,3,3,3,4}));// Expected Output: 7// Test case 3: No duplicatesSystem.out.println(removeDuplicatesII(newint[] {1,2,3,4,5}));// Expected Output: 5// Test case 4: Empty arraySystem.out.println(removeDuplicatesII(newint[] {}));// Expected Output: 0// Test case 5: All elements are the sameSystem.out.println(removeDuplicatesII(newint[] {2,2,2,2,2}));// Expected Output: 2// Test case 6: Single element arraySystem.out.println(removeDuplicatesII(newint[] {7}));// Expected Output: 1

About

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.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp