Question

In: Computer Science

This function will receive a list of elements with duplicate elements, this function should remove the...

This function will receive a list of elements with duplicate elements, this function should remove the duplicate elements in the list and return a list without duplicate elements. The elements in the returned list must be in the same order that they were found in the list the function received. A duplicate element is an element found more than one time in the specified list.

JAVA

Solutions

Expert Solution

If you have any problem with the code feel free to comment.

Program

import java.util.ArrayList;
import java.util.function.Predicate;

public class Test {
        public static void main(String[] args) {
                ArrayList<Integer> list = new ArrayList<>();
                list.add(1);
                list.add(2);
                list.add(1);
                list.add(5);
                list.add(2);
                list.add(4);
                list.add(2);
                System.out.println("Duplicate list: " + list);
                System.out.println("Non duplicate list: " + removeDuplicates(list));
        }

        public static <T> ArrayList<T> removeDuplicates(ArrayList<T> list) {
                ArrayList<T> newList = new ArrayList<>(list);

                // replacing the duplicate items with null
                for (T item : newList) {
                        if (item == null)
                                continue;
                        int count = 0;
                        for (int i = 0; i < newList.size(); i++) {
                                if (item.equals(newList.get(i)))
                                        count++;
                                if (item.equals(newList.get(i)) && count > 1) {
                                        newList.set(i, null);
                                }
                        }
                }

                // removing all null values from the list
                newList.removeIf((Predicate<T>) ((t) -> t == null));

                return newList;
        }

}

Output


Related Solutions

Task 1: Remove Number Complete the function remove number such that given a list of integers...
Task 1: Remove Number Complete the function remove number such that given a list of integers and an integer n, the function removes every instance of n from the list. Remember that this function needs to modify the list, not return a new list. Task 2: Logged List The log2() function is one of an algorithm designer’s favourite functions. You’ll learn more about this later, but briefly – if your input size is 1048576 elements, but you only look at...
This function will be given a list of strings and a character. You must remove all...
This function will be given a list of strings and a character. You must remove all occurrences of the character from each string in the list. The function should return the list of strings with the character removed. Signature: public static ArrayList<String> removeChar(String pattern, ArrayList<String> list)
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should...
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should assume that the list is already in ascending order. The function should insert the number into the correct position of the list so that the list stays in ascending order. It should modify the list, not build a new list. It does not need to return the list, because it is modifying it.   Hint: Use a whlie loop and list methods lst = [1,3,5,7]...
C LANGUAGE ONLY Write a C program to count the total number of duplicate elements in...
C LANGUAGE ONLY Write a C program to count the total number of duplicate elements in an array. Enter the number of elements to be stored in the array: 3 Input 3 elements in the arrangement: element [0]: 5 element [1]: 1 element [2]: 1 Expected output: The total number of duplicate elements found in the array is: 1
#3 A collection represents a group of objects, known as its elements. Some collections allow duplicate...
#3 A collection represents a group of objects, known as its elements. Some collections allow duplicate elements and others do not. Some are ordered and others unordered. Create an Interface MyCollection which is maximum general (abstract) collection possible. (java oop)-> laboratory work
1. Explain four of the basic principles of lending. 2. List twelve elements that should be...
1. Explain four of the basic principles of lending. 2. List twelve elements that should be included in a banks credit policy.
I need to implement a method that removes any duplicate items in a list and returns...
I need to implement a method that removes any duplicate items in a list and returns the new values    private static linkedlist removeDuplicates(linkedlist list) {    return list; }
Write a c++ member function that attempts to insert a NON DUPLICATE element to a doubly...
Write a c++ member function that attempts to insert a NON DUPLICATE element to a doubly linked list, After the attempted insertion return the SIZE of the doubly linked list whether or not the insertion was successful.
1. List ten (10) elements that should be included in your institution's credit policy. 2. What...
1. List ten (10) elements that should be included in your institution's credit policy. 2. What are the advantages of a credit institution having a written loan policy? 3. State the four (4) things an effective credit risk management system should do. 5. Explain, in general terms, how to conduct a credit risk assessment.
Under the MBCA, what are the steps that should be taken by a shareholder to remove...
Under the MBCA, what are the steps that should be taken by a shareholder to remove a director? Your paper should address in detail such issues as authority to act, notice, meeting and voting requirements.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT