Question

I need help with this Java exercise. I'm not allowed to use imports and I tried...

I need help with this Java exercise. I'm not allowed to use imports and I tried coding it but ended up failing.

        /**
                Given a String array, return an String array with duplicate Strings removed if the array contains duplicate values. <br>
                Note: Capital letters count. <br> <br>

                removeDuplicateStrings({"a"}) -> {"a"} <br>
                removeDuplicateStrings({"a", "b", "c", "d"}) -> {"a", "b", "c", "d"} <br>
                removeDuplicateStrings({"a", "a"})  -> {"a"} <br>
                removeDuplicateStrings({"A", "a"})  -> {"A", "a"} <br>
                removeDuplicateStrings({"these", "are", "the", "times"}) -> {"these", "are", "the", "times"} <br>
                removeDuplicateStrings({"these", "times", "are", "the", "times", "they", "are"}) -> {"these", "times", "are", "the", "they"} <br>
                @param strings String[] an array of integers.
                @return String[] An array with the duplicates removed.
        **/
        public static String[] removeDuplicateStrings(String[] strings) {
                //your code here
                        return strings;
                }//end removeDuplicateStrings
0 0
Add a comment Improve this question Transcribed image text
Answer #1

public class RenoveStringDup {
   public static void main(String[] args) {
       String arr1[]={"these", "times", "are", "the", "times", "they", "are"};
       printArray(arr1);
       printArray(removeDuplicateStrings(arr1));
   }

   private static void printArray(String[] arr) {
       for(String s:arr)
           System.out.print(s+" ");
       System.out.println();
   }

   public static String[] removeDuplicateStrings(String[] strings) {
       int count=0;
       for (int s = 0; s < strings.length - 1; s++) {
           for (int m = s + 1; m < strings.length; m++) {

               if (strings[s] != null && strings[s].equals(strings[m])) {
                   // array = ArrayUtils.removeElement(array, array[s]); --m;??
                   strings[m] = null; // Mark for deletion later on
                   count++;
               }
           }
       }
       String res[]=new String[strings.length-count];
       for (int s = 0,i=0; s < strings.length - 1; s++) {
           if(strings[s]!=null)
               res[i++]=strings[s];
       }
       return res;
   }// end remove
}

E Console 3 temid> RenoveStringDup [Java Application] C:1Soft Peaein64-4.5.2.2Peg these times are the times they are these ti

Add a comment
Know the answer?
Add Answer to:
I need help with this Java exercise. I'm not allowed to use imports and I tried...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hey, I was wondering if there’s another way to make these methods not reliable to imports...

    Hey, I was wondering if there’s another way to make these methods not reliable to imports such as “import java.util.Arrays” and “import java.util.Hash” I am still new in coding and would like to know if there’s another way to do it! Here is the code! (Thank you in advance and I will really appreciate it :) ) /** This exercise involves implementing several methods. Stubs for each method with documentation are given here. It is your task to fill out...

  • /** Given an int array, return true if the array contains duplicate values. duplicateInts({3}) -> false...

    /** Given an int array, return true if the array contains duplicate values. duplicateInts({3}) -> false duplicateInts({1, 2}) -> false duplicateInts({7, 7}) -> true duplicateInts({1, 2, 3, 4, 5}) -> false duplicateInts({1, 2, 3, 2, 4, 5}) -> true **/ public static boolean duplicateInts(int[] numbers) { //your code here return false; }//end duplicateInts /** Given a String array, return true if the array contains duplicate values. Note: Capital letters count duplicateStrings({"a"}) -> false duplicateStrings({"a", "b", "c", "d"}) -> false duplicateStrings({"a",...

  • /** Given an int array, return an int array with duplicate ints removed if the array...

    /** Given an int array, return an int array with duplicate ints removed if the array contains duplicate values. <br> <br> removeDuplicateInts({3}) -> {3} <br> removeDuplicateInts({1, 2}) -> {1, 2} <br> removeDuplicateInts({7, 7}) -> {7} <br> removeDuplicateInts({1, 7, 1, 7, 1}) -> {1, 7} <br> removeDuplicateInts({1, 2, 3, 4, 5}) -> {1, 2, 3, 4, 5}) <br> removeDuplicateInts({1, 2, 3, 2, 4, 2, 5, 2}) -> {1, 2, 3, 4, 5} <br> @param numbers int[] an array of integers. @return...

  • Given java code is below, please use it! import java.util.Scanner; public class LA2a {      ...

    Given java code is below, please use it! import java.util.Scanner; public class LA2a {       /**    * Number of digits in a valid value sequence    */    public static final int SEQ_DIGITS = 10;       /**    * Error for an invalid sequence    * (not correct number of characters    * or not made only of digits)    */    public static final String ERR_SEQ = "Invalid sequence";       /**    * Error for...

  • Hi I really need help with the methods for this lab for a computer science class....

    Hi I really need help with the methods for this lab for a computer science class. Thanks Main (WordTester - the application) is complete and only requires uncommenting to test the Word and Words classes as they are completed. The needed imports, class headings, method headings, and block comments are provided for the remaining classes. Javadocs are also provided for the Word and Words classes. Word The Word class represents a single word. It is immutable. You have been provided...

  • Please help me with this Java project. I'm trying to create a loop so if user...

    Please help me with this Java project. I'm trying to create a loop so if user enter value > 12, will prompt them a message and take them back to "How many elements do you wan to enter?". Thanks public static void main(String[] args) {        Scanner sc = new Scanner(System.in);           int i, j;       System.out.print("\n How meny elements do you want to enter? (N should be no more than 12) ");    int num...

  • I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][]...

    I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][] t) A method that returns true if from left to right in any row, the integers are increasing, otherwise false. - columnValuesIncrease(int[][] t) A method that returns true if from top to bottom in any column, the integers are increasing, otherwise false. - isSetOf1toN(int[][] t) A method that returns true if the set of integers used is {1, 2, . . . , n}...

  • Need help coding this List. Lists are a lot like arrays, but you’ll be using get,...

    Need help coding this List. Lists are a lot like arrays, but you’ll be using get, set, add, size and the like instead of the array index operators [].​ package list.exercises; import java.util.List; public class ListExercises {    /**    * Counts the number of characters in total across all strings in the supplied list;    * in other words, the sum of the lengths of the all the strings.    * @param l a non-null list of strings   ...

  • In java write a command-line program that helps to decrypt a message that has been encrypted...

    In java write a command-line program that helps to decrypt a message that has been encrypted using a Caesar cipher1. Using this method, a string may contain letters, numbers, and other ASCII characters, but only the letters (upper- and lower-case) are encrypted – a constant number, the shift, is added to the ASCII value of each letter and when letters are shifted beyond ‘z’ or ‘Z’ they are wrapped around (e.g. “Crazy?” becomes “Etcba?” when shifted by 2). When your...

  • I need help making this work correctly. I'm trying to do an array but it is...

    I need help making this work correctly. I'm trying to do an array but it is drawing from a safeInput class that I am supposed to use from a previous lab. The safeInput class is located at the bottom of this question I'm stuck and it is not printing the output correctly. The three parts I think I am having most trouble with are in Bold below. Thanks in advance. Here are the parameters: Create a netbeans project called ArrayStuff...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT