Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free triallinda wu
3,016 Pointsremove duplicate from the array
Im trying to understand the following sample code of removing the duplicate numbers in the sorted array:
package Test;
public class MyDuplicateElements {
public static int[] removeDuplicates(int[] input){
int j = 0;
int i = 1;
//return if the array length is less than 2
if(input.length < 2){
return input;
}
while(i < input.length){
if(input[i] == input[j]){
i++;
}else{
//when input[i]!=input[j]
//use i first, then increment later.< post increment.>
//use ++j first, <pre-increment>
//in a sense when first two number in the array are not same, j++, index 1 of array name <input> is assigned by input index 2. somehow it is missing the 0 index when the first two element are not same?
input[++j] = input[i++];
}
}
int[] output = new int[j+1];
for(int k=0; k<output.length; k++){
//output[0]=input[0], when the first two number are not same, it is using the zero index of the array, while the length of output array is j+1; output[k] = input[k]; }
return output;
}
public static void main(String a[]){
int[] input1 = {1,2,2,3};
int[] output = removeDuplicates(input1);
for(int i:output){
System.out.print(i+" ");
}
}
}
I can understand in the while loop tries to get the no-duplicate length of the input array and rearrange the array with index variable j. And output array is using the new length with unique numbers.
I'd appreciated if someone can help me understand those comment area in case of first and second numbers in the array are not equal, why j=0 value never get assigned. It only assigned the value when index is j=1 to input array index 2.
However, the zero index input array value later is assigned to output array.
package com.example;
import java.util.Date;
public class BlogPost {
private String mAuthor;
private String mTitle;
private String mBody;
private String mCategory;
private Date mCreationDate;
public BlogPost(String author, String title, String body, String category, Date creationDate) {
mAuthor = author;
mTitle = title;
mBody = body;
mCategory = category;
mCreationDate = creationDate;
}
public String getAuthor() {
return mAuthor;
}
public String getTitle() {
return mTitle;
}
public String getBody() {
return mBody;
}
public String getCategory() {
return mCategory;
}
public Date getCreationDate() {
return mCreationDate;
}
}
import com.example.BlogPost;
public class TypeCastChecker {
/***************
I have provided 2 hints for this challenge.
Change `false` to `true` in one line below, then click the "Check work" button to see the hint.
NOTE: You must set all the hints to false to complete the exercise.
****************/
public static boolean HINT_1_ENABLED = false;
public static boolean HINT_2_ENABLED = false;
public static String getTitleFromObject(Object obj) {
// Fix this result variable to be the correct string.
String result = "";
return result;
}
}