Question

In: Computer Science

Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice...

Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

Example 1:

 
 

Given nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3 respectively. It doesn't matter what you leave beyond the returned length.

Example 2:

 
 

Given nums = [0,0,1,1,1,1,2,3,3], Your function should return length = 7, with the first seven elements of nums being modified to 0, 0, 1, 1, 2, 3 and 3 respectively. It doesn't matter what values are set beyond the returned length.

Clarification:

Confused why the returned value is an integer but your answer is an array?

Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.

CODE::

import java.util.Scanner;

class Main {
public static int removeDuplicates(int[] nums){
// your code goes here
if(nums.length == 0) return 0;
if(nums.length == 1) return 1;
if(nums.length == 2) return 2;
  
int index = 0;
if(nums.length > 2){
for(int i = 0; i < nums.length - 2; i++){
if(nums[i] != nums[i+1]){
nums[index] = nums[i];
index++;
} else{
if(nums[i+1] != nums[i+2]){
nums[index] = nums[i];
index++;
}
}
}
  
if(index <= nums.length - 2){
nums[index] = nums[nums.length - 2];
index++;
nums[index] = nums[nums.length - 1];
}
return index + 1;
}else
return -1;
}
public static void main(String[] args) {
// take array input here
int[] nums = new int[]{1, 1, 1, 2, 2, 3};
// output goes here
int newLength = removeDuplicates(nums);
  
if(newLength <= 0){
System.out.println("Array is empty");
}
else{
for(int i = 0; i < newLength; i++){
System.out.plint(nums[i] + " ");
}
System.out.println();
}
  
}
}

Solutions

Expert Solution

Program ScreenShot :

Program code:

import java.util.Scanner;

class Main {
   public static int removeDuplicates(int[] nums) {
       // your code goes here
       if (nums.length == 0)
           return 0;
       if (nums.length == 1)
           return 1;
       if (nums.length == 2)
           return 2;

       int index = 0;
       if (nums.length > 2) {
           for (int i = 0; i < nums.length - 2; i++) {
               if (nums[i] != nums[i + 1]) {
                   nums[index] = nums[i];
                   index++;
               } else {
                   if (nums[i + 1] != nums[i + 2]) {
                       nums[index] = nums[i];
                       index++;
                   }
               }
           }

           if (index <= nums.length - 2) {
               nums[index] = nums[nums.length - 2];
               index++;
               nums[index] = nums[nums.length - 1];
           }
           return index + 1;
       } else
           return -1;
   }

   public static void main(String[] args) {
       // take array input here
       int[] nums = new int[] { 1, 1, 1, 2, 2, 3 };
       // output goes here
       int newLength = removeDuplicates(nums);
       System.out.println("length = " + newLength);
       System.out.print("The first " + newLength + " of nums is : ");
       if (newLength <= 0) {
           System.out.println("Array is empty");
       } else {
           for (int i = 0; i < newLength; i++) {
               System.out.print(nums[i]);
               if(i < newLength -1)
                   System.out.print(",");
           }
          
       }

   }
}

Sample Output :


Related Solutions

Given a sorted array with lot of duplicates, write a problem to search specific element m....
Given a sorted array with lot of duplicates, write a problem to search specific element m. If it’s included in the A, return its minimal index, otherwise return -1. For example A = {0, 0, 1, 1, 1, 2, 3, 3, 4, 4, 4, 4, 4}, if we search 4, you program should return 8. You are only allowed to use binary search. Linear search is not allowed here.
This Array implementation allows duplicates. Add a method that searches the array and remove all the...
This Array implementation allows duplicates. Add a method that searches the array and remove all the values in the array that does not have a duplicate. void removeNoDups( ) ( 12 points) For example if array had elements 100 200 100 100 200 400 500 300, once this new method is run it should return 100 200 100 100 200 removing 400, 500 and 300 which do not have duplicate values in the array. So in short this method allows...
This Array implementation allows duplicates. Add a method that searches the array and remove all the...
This Array implementation allows duplicates. Add a method that searches the array and remove all the values in the array that does not have a duplicate. void removeNoDups( ) ( 12 points) For example if array had elements 100 200 100 100 200 400 500 300, once this new method is run it should return 100 200 100 100 200 removing 400, 500 and 300 which do not have duplicate values in the array. So in short this method allows...
This all has to be in javascript A common way to remove duplicates from an array...
This all has to be in javascript A common way to remove duplicates from an array is to convert it to a set and then convert it back to an array. We will do that here. Please use these variables and data structures: var input = [3, 4, 2, 2, 4, 5, 3, 1, 3, 6]; var set = new Set(); 1. Get each element from the input array and add it to the set. 2. Get the elements from...
6. Given the following array of characters: REALITYSHOWShow how this array will be sorted with the...
6. Given the following array of characters: REALITYSHOWShow how this array will be sorted with the help of : (a) insertion sort; (b) selection sort; (c) bubble sort with swaps counting; (d) bubble sort without swaps counting subject design and analysis of algorithms answer should be like eg p u r p l e p r u p l e
Given an integer array sorted in non-decreasing order, there is exactly one integer in the array...
Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time. Return that integer. Input: arr = [1,2,2,6,6,6,6,7,10] Output:
in C programming language Write a function removeDups that removes all duplicates in a given array...
in C programming language Write a function removeDups that removes all duplicates in a given array of type int. Sample Test Case: input -> {1,2,2,2,3,3,4,2,4,5,6,6} output -> {1,2,3,4,5,6,0,0,0,0,0,0} More specifically, the algorithm should only keep the first occurance of each element in the array, in the order they appear. In order to keep the array at the same length, we will replace the removed elements with zeros, and move them to the end of the array.
We are given a non sorted array so that the values are not pairwise disjoint (the...
We are given a non sorted array so that the values are not pairwise disjoint (the same values may appear in many entries). Say that we can find the k-th smallest element number in the array in time O(n) for any 1 <= k <= n. Give an algorithm that finds if there is a value that appears at least n/3 times. Please explain the algorithm in words and analyze the run time.
THIS IS FOR JAVA Given an oversize array of size words and a word to remove,...
THIS IS FOR JAVA Given an oversize array of size words and a word to remove, write a method that returns the array with each occurrence of the given word removed. Shift the remaining words in the nonempty part of the array to the left so that each occurrence of the given word is overwritten. (Leave the words in the empty part of the array unchanged.) Hint: To understand the test cases, note that the size (but not the capacity)...
in java code In the class Hw2, write a method removeDuplicates that given a sorted array,...
in java code In the class Hw2, write a method removeDuplicates that given a sorted array, (1) removes the duplicates so that each distinct element appears exactly once in the sorted order at beginning of the original array, and (2) returns the number of distinct elements in the array. The following is the header of the method: public static int removeDuplicates(int[ ] A) For example, on input A=0, 0, 1, 1, 1, 2, 2, 3, 3, 4, your method should:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT