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]...
Write a function in Python 3 (v. 6175+) called multiplicity00794. The function should receive alimit number...
Write a function in Python 3 (v. 6175+) called multiplicity00794. The function should receive alimit number and return: • how many multiples of 3 or 5 or 7 there are that are less than or equal to the specified limit. • the sum of the multiples of 3 or 5 or 7 that are less than or equal to the specified limit. • the product of the multiples of 3 or 5 or 7 that are less than or equal...
This is python: #Write a function called count_positive_evens. This function #should take as input a list...
This is python: #Write a function called count_positive_evens. This function #should take as input a list of integers, and return as #output a single integer. The number the function returns #should be the count of numbers from the list that were both #positive and even. # #For example: # # count_positive_evens([5, 7, 9, 8, -1, -2, -3]) -> 1 # count_positive_evens([2, 4, 6, 8, 10, 12, 15]) -> 6 # count_positive_evens([-2, -4, -6, -8, -10, 1]) -> 0 # #0...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT