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

Fixed Exercise 19_03 (Actually removes duplicates & returns new list)#15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
SleekPanther wants to merge1 commit intojsquared21:master
base:master
Choose a base branch
Loading
fromSleekPanther:master
Open
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletionsExercise_19/Exercise_19_03/Exercise_19_03.java
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
/*********************************************************************************
* (Distinct elements in ArrayList) Write the following method that returns a new *
* ArrayList. The new list contains the non-duplicate elements from the original *
* list. *
* *
* public static <E> ArrayList<E> removeDuplicates(ArrayList<E> list) *
*********************************************************************************/
/**************************************************************************************
* (Distinct elements in ArrayList) Write the following method that returns a new *
* ArrayList. The new list contains the non-duplicate elements from the original list. *
* *
* public static <E> ArrayList<E> removeDuplicates(ArrayList<E> list) *
**************************************************************************************/
import java.util.ArrayList;

public class Exercise_19_03 {
/** Removes duplicate elements from an array list */
public static <E extends Comparable<E>>
ArrayList<E> removeDuplicates(ArrayList<E> list) {
for (int i = 0; i < list.size() - 1; i++) {
for (int j = i + 1; j < list.size(); j++) {
if (list.get(i).compareTo(list.get(j)) == 0)
list.remove(j);
public static <E extends Comparable<E>> ArrayList<E> removeDuplicates(ArrayList<E> list) {
ArrayList<E> newList = new ArrayList<E>();
if(!list.isEmpty()){
newList.add(list.get(0));//adds 1st item from reference list
for (int i = 1; i < list.size(); i++) {//loop starts from 1
boolean shouldAddToNewList = true;
for (int j = 0; j < newList.size(); j++) {
if (list.get(i).compareTo(newList.get(j)) == 0){
shouldAddToNewList = false;
}
}
if(shouldAddToNewList){
newList.add(list.get(i));
}
}
}
returnlist;
}
}
returnnewList;
}
}

[8]ページ先頭

©2009-2025 Movatter.jp