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...

  • I need help with my Java code. A user enters a sentence and the program will...

    I need help with my Java code. A user enters a sentence and the program will tell you what words were duplicated and how many times. If the same word is in the sentence twice, but one is capitalized, it will not count it as a duplicate. How can I make the program read the same word as the same word, even if one is capitalized and the other is not? For example, "the" and "The" should be counted as...

  • /** 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...

  • 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...

  • 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...

  • 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   ...

  • I need help with my code when I run my code running the wrong thing like...

    I need help with my code when I run my code running the wrong thing like this After downSize() words.length=60003 wordCount=60003 vowelCount=206728 this is my code here import java.io.*; import java.util.*; public class Project02 {    static final int INITIAL_CAPACITY = 10;    public static void main (String[] args) throws Exception    {        // ALWAYS TEST FIRST TO VERIFY USER PUT REQUIRED INPUT FILE NAME ON THE COMMAND LINE        if (args.length < 1 )       ...

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