Question

In: Computer Science

Java Write a method that removes duplicates from an array of strings and returns a new...

Java

Write a method that removes duplicates from an array of strings and returns a new array, free of any duplicate strings.

Solutions

Expert Solution

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
 
public class Main {
 public static String[] remove_duplicates(String[] courses){
     
     /*
         * convert array to list and then add all
         * elements to LinkedHashSet. LinkedHashSet
         * will automatically remove all duplicate elements. 
         */
        LinkedHashSet<String> linkedhashset =  
                new LinkedHashSet<String>(Arrays.asList(courses));
        
       
        String[] newArray = linkedhashset.toArray(new String[ linkedhashset.size() ]);
        
       
     return newArray;
 }
    public static void main(String[] args) {
        //original array
        String[] courses = {
                "c++",
                "c",
                "java",
                "c", //duplicate
                "python",
                "java"    //duplicate            
        };
        System.out.print("     original array             : ");
       for(int i=0;i<courses.length;i++){
           System.out.print(courses[i]+",");
       }
        
       String[] newArray=new String[courses.length];
       newArray=remove_duplicates(courses); 
       System.out.println();
         System.out.print("array after removing duplicates : ");
       for(int i=0;i<newArray.length;i++){
           System.out.print(newArray[i]+",");
       }
 
    }
}

.........................

  • Java LinkedHashSet class contains unique elements.
  • when string array is added to linkedhashset ,it automatically removes duplicate elements.This is advantage of using collections

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]
IN JAVA Write a program with a method that returns an array. The method should accept...
IN JAVA Write a program with a method that returns an array. The method should accept as input a comma-delimited string with three values from a user. The array should store each value in a different element. Use Try..Catch error handling and print any failure messages, or print success from within method if the execution is successful (see Chapter 13 in the text). Call the method from the main method of the program to demonstrate its functionality by looping through...
in C programming language Write a function removeDups that removes all duplicates in a given array...
in C programming language Write a function removeDups that removes all duplicates in a given array of type int. Sample Test Case: input -> {1,2,2,2,3,3,4,2,4,5,6,6} output -> {1,2,3,4,5,6,0,0,0,0,0,0} More specifically, the algorithm should only keep the first occurance of each element in the array, in the order they appear. In order to keep the array at the same length, we will replace the removed elements with zeros, and move them to the end of the array.
Write a Java method that returns the index of the largest element in an array of...
Write a Java method that returns the index of the largest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header: 
 public static int indexOfLargestElement(double[] array)
 Write a test program that prompts the user to enter ten numbers, invokes this
method to return the index of the largest element, and displays the index.
Using Java Write a method that returns the index of the smallest element in an array...
Using Java Write a method that returns the index of the smallest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header:   public static int indexOfSmallestElement (double[] array)
Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
Write a Java method that removes any duplicate elements from an ArrayList of integers. The method...
Write a Java method that removes any duplicate elements from an ArrayList of integers. The method has the following header(signature): public static void removeDuplicate(ArrayList<Integer> list) Write a test program (with main method) that prompts the user to enter 10 integers to a list and displays the distinct integers separated by exactly one space. Here is what the input and output should look like:      Enter ten integers: 28 4 2 4 9 8 27 1 1 9      The distinct...
write a java prog that include an array of strings of size 50    String[] strings...
write a java prog that include an array of strings of size 50    String[] strings = new String[50]; then find the max length of the inputs inside a method keep reading from the user until user enters keyword to stop input : may ala jony ram asd fgghff daniel jwana output : max length : 10
The split method in the String class returns an array of strings consisting of the substrings...
The split method in the String class returns an array of strings consisting of the substrings split by the delimiters. However, the delimiters are not returned. Implement the following new method that returns an array of strings consisting of the substrings split by the matching delimiters, including the matching delimiters. public static String[] split(String s, String regex) For example, split("ab#12#453", "#") returns ab, #, 12, #, 453 in an array of String, and split("a?b?gf#e", "[?#]") returns a, ?, b, ?,...
IN JAVA write a program that creates an array of strings with 8 people in it....
IN JAVA write a program that creates an array of strings with 8 people in it. Second,  Assign a random rank between 1 to 8 to each of the players. The rankings do not change throughout the tournament. Finally, Sort the players based on the rankings and print the data (show rankings of players, in square brackets, at every step after they are ranked). USING JAVA COLLECTIONS IS NOT ALLOWED
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT