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

JAVA Write a method that removes the duplicate elements from an array list of integers using...
JAVA Write a method that removes the duplicate elements from an array list of integers using the following header: public static void removeDuplicate(ArrayList list) Write a test program that prompts the user to enter 10 integers to a list and displays the distinct integers separated by exactly one space. Here is a sample run: Enter ten integers: 10 20 30 20 20 30 50 60 100 9 The distinct integers are: [10, 20, 30, 50, 60, 100, 9]
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...
method to remove all elements frrom my linked list (java)
method to remove all elements frrom my linked list (java)
Complete the code that inserts elements into a list. The list should always be in an...
Complete the code that inserts elements into a list. The list should always be in an ordered state. #include <stdio.h> #include <stdlib.h> /* list of nodes, each with a single integer */ struct element { struct element *next; int value; }; /* protypes for functions defined after main */ struct element *elementalloc(void); struct element *listinitialize(); struct element *insertelement(struct element *, int); void printlist(struct element *); /* main * Creates an ordered list * Elements added to the list must be...
Complete the code that inserts elements into a list. The list should always be in an...
Complete the code that inserts elements into a list. The list should always be in an ordered state. #include <stdio.h> #include <stdlib.h> /* list of nodes, each with a single integer */ struct element { struct element *next; int value; }; /* protypes for functions defined after main */ struct element *elementalloc(void); struct element *listinitialize(); struct element *insertelement(struct element *, int); void printlist(struct element *); /* main * Creates an ordered list * Elements added to the list must be...
Complete the code that inserts elements into a list. The list should always be in an...
Complete the code that inserts elements into a list. The list should always be in an ordered state. #include <stdio.h> #include <stdlib.h> /* list of nodes, each with a single integer */ struct element { struct element *next; int value; }; /* protypes for functions defined after main */ struct element *elementalloc(void); struct element *listinitialize(); struct element *insertelement(struct element *, int); void printlist(struct element *); /* main * Creates an ordered list * Elements added to the list must be...
Complete the code that inserts elements into a list. The list should always be in an...
Complete the code that inserts elements into a list. The list should always be in an ordered state. #include <stdio.h> #include <stdlib.h> /* list of nodes, each with a single integer */ struct element { struct element *next; int value; }; /* protypes for functions defined after main */ struct element *elementalloc(void); struct element *listinitialize(); struct element *insertelement(struct element *, int); void printlist(struct element *); /* main * Creates an ordered list * Elements added to the list must be...
Complete the code that inserts elements into a list. The list should always be in an...
Complete the code that inserts elements into a list. The list should always be in an ordered state. ----------------------------------------------------------------------------------------------------------------- #include <stdio.h> #include <stdlib.h> /* list of nodes, each with a single integer */ struct element { struct element *next; int value; }; /* protypes for functions defined after main */ struct element *elementalloc(void); struct element *listinitialize(); struct element *insertelement(struct element *, int); void printlist(struct element *); /* main * Creates an ordered list * Elements added to the list must...
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)
Complete the code(in C) that inserts elements into a list. The list should always be in...
Complete the code(in C) that inserts elements into a list. The list should always be in an ordered state. #include <stdio.h> #include <stdlib.h> /* list of nodes, each with a single integer */ struct element { struct element *next; int value; }; /* protypes for functions defined after main */ struct element *elementalloc(void); struct element *listinitialize(); struct element *insertelement(struct element *, int); void printlist(struct element *); /* main * Creates an ordered list * Elements added to the list must...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT