Question

/** 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", "a"})  -> true
                duplicateStrings({"A", "a"})  -> false
                duplicateStrings({"these", "are", "the", "times"}) -> false
                duplicateStrings({"these", "are", "the", "times", "they", "are"}) -> true
                duplicateStrings({"my", "dear"}, {"Aunt", "Sally"}) -> {"Aunt", "Sally", "dear", "my"}
                duplicateStrings({"Irving", "washington"}, {"Irving", "berlin"})  -> {"Irving", "Irving", "berlin", "washington"}
        **/
        public static boolean duplicateStrings(String[] strings) {
                //your code here
                return false;
        }//end duplicateStrings

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

                removeDuplicateInts({3})  -> {3}
                removeDuplicateInts({1, 2})  -> {1, 2}
                removeDuplicateInts({7, 7})  -> {7}
                removeDuplicateInts({1, 7, 1, 7, 1})  -> {1, 7}
                removeDuplicateInts({1, 2, 3, 4, 5})  -> {1, 2, 3, 4, 5})
                removeDuplicateInts({1, 2, 3, 2, 4, 2, 5, 2})  -> {1, 2, 3, 4, 5}
        **/
        public static int[] removeDuplicateInts(int[] numbers) {
                //your code here
                        return numbers;
                } // end removeDuplicateInts


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

                removeDuplicateStrings({"a"}) -> {"a"}
                removeDuplicateStrings({"a", "b", "c", "d"}) -> {"a", "b", "c", "d"}
                removeDuplicateStrings({"a", "a"})  -> {"a"}
                removeDuplicateStrings({"A", "a"})  -> {"A", "a"}
                removeDuplicateStrings({"these", "are", "the", "times"}) -> {"these", "are", "the", "times"}
                removeDuplicateStrings({"these", "times", "are", "the", "times", "they", "are"}) -> {"these", "times", "are", "the", "they"})
        **/
        public static String[] removeDuplicateStrings(String[] strings) {
                //your code here
                        return strings;
                }//end removeDuplicateStrings


        /**
                Given two int arrays return true if the arrays contain the same values in the same order.
                Note: Order matters, see the third example

                compare2IntArrays({3, 4}, {1}) -> false
                compare2IntArrays({1, 2}, {1, 2}) -> true
                compare2IntArrays({1, 2}, {2, 1}) -> false
                compare2IntArrays({1, 2, 3, 4}, {1, 2, 3, 4}) -> true
        **/
        public static boolean compare2IntArrays(int[] firstNumbers, int[] secondNumbers) {
                //your code here
                        return false;
                }//end compare2IntArrays


        /**
                Given two String arrays return true if the arrays contain the same values in the same order.
                Note: Order matters, see the forth example
                Note: Capatil letters matter, see the final example

                compare2StringArrays({"and"}, {"or"})  -> false
                compare2StringArrays({"and", "but"}, {"or"})  -> false
                compare2StringArrays({"a", "b", "c", "d"}, {"a", "b", "c", "d"})  -> true
                compare2StringArrays({"a", "b", "c", "d"}, {"d", "c", "b", "d"})  -> false
                compare2StringArrays({"a", "b", "c", "d"}, {"A", "b", "C", "d"})  -> false
                compare2StringArrays({"Aunt", "Sally"}, {"Aunt", "Sally"}) -> true
                compare2StringArrays({"Aunt", "Sally"}, {"Aunt", "sally"}) -> false
        **/
        public static boolean compare2StringArrays(String[] firstStrings, String[] secondStrings) {
                //your code here
                        return false;
                }//end TESTcompare2StringArrays


}//end Functions2

======================================================================================

PLEASE DO NOT USE SORT METHODE JUST SIMPLE CODING TO SOLVE THIS CODE

THANKS

=========================================================================================

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.ArrayList;

public class Functions2 {

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

       for (int i = 0; i < numbers.length; i++) {
           for (int j = 0; j < numbers.length; j++) {

               if (i != j) {

                   if (numbers[i] == numbers[j])
                       return true;
               }
           }
       }
       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", "a"}) -> true duplicateStrings({"A", "a"}) ->
   * false duplicateStrings({"these", "are", "the", "times"}) -> false
   * duplicateStrings({"these", "are", "the", "times", "they", "are"}) -> true
   * duplicateStrings({"my", "dear"}, {"Aunt", "Sally"}) -> {"Aunt", "Sally",
   * "dear", "my"} duplicateStrings({"Irving", "washington"}, {"Irving",
   * "berlin"}) -> {"Irving", "Irving", "berlin", "washington"}
   **/
   public static boolean duplicateStrings(String[] strings) {
       // your code here
       for (int i = 0; i < strings.length; i++) {
           for (int j = 0; j < strings.length; j++) {

               if (i != j) {

                   if (strings[i].compareTo(strings[j]) == 0)
                       return false;
               }
           }
       }
       return false;
   }// end duplicateStrings

   /**
   * Given an int array, return an int array with duplicate ints removed if the
   * array contains duplicate values.
   *
   * removeDuplicateInts({3}) -> {3} removeDuplicateInts({1, 2}) -> {1, 2}
   * removeDuplicateInts({7, 7}) -> {7} removeDuplicateInts({1, 7, 1, 7, 1}) ->
   * {1, 7} removeDuplicateInts({1, 2, 3, 4, 5}) -> {1, 2, 3, 4, 5})
   * removeDuplicateInts({1, 2, 3, 2, 4, 2, 5, 2}) -> {1, 2, 3, 4, 5}
   **/
   public static int[] removeDuplicateInts(int[] numbers) {
       // your code here

       if (duplicateInts(numbers)) {

           ArrayList<Integer> result = new ArrayList<>();

           for (int i = 0; i < numbers.length; i++) {

               if (result.size() > 0) {

                   boolean flag = true;
                   for (int j = 0; j < result.size(); j++) {

                       if (result.get(j) == numbers[i])
                           flag = false;
                   }

                   if (flag)
                       result.add(numbers[i]);
               } else
                   result.add(numbers[i]);
           }

           numbers = new int[result.size()];

           for (int i = 0; i < result.size(); i++)
               numbers[i] = result.get(i);

       }

       return numbers;
   } // end removeDuplicateInts

   /**
   * Given a String array, return an String array with duplicate Strings removed
   * if the array contains duplicate values. Note: Capital letters count
   *
   * removeDuplicateStrings({"a"}) -> {"a"} removeDuplicateStrings({"a", "b", "c",
   * "d"}) -> {"a", "b", "c", "d"} removeDuplicateStrings({"a", "a"}) -> {"a"}
   * removeDuplicateStrings({"A", "a"}) -> {"A", "a"}
   * removeDuplicateStrings({"these", "are", "the", "times"}) -> {"these", "are",
   * "the", "times"} removeDuplicateStrings({"these", "times", "are", "the",
   * "times", "they", "are"}) -> {"these", "times", "are", "the", "they"})
   **/
   public static String[] removeDuplicateStrings(String[] strings) {
       // your code here
       if (duplicateStrings(strings)) {

           ArrayList<String> result = new ArrayList<>();

           for (int i = 0; i < strings.length; i++) {

               if (result.size() > 0) {

                   boolean flag = true;
                   for (int j = 0; j < result.size(); j++) {

                       if (result.get(j).compareTo(strings[i]) == 0)
                           flag = false;
                   }

                   if (flag)
                       result.add(strings[i]);
               } else
                   result.add(strings[i]);
           }

           strings = new String[result.size()];

           for (int i = 0; i < result.size(); i++)
               strings[i] = result.get(i);

       }
       return strings;
   }// end removeDuplicateStrings

   /**
   * Given two int arrays return true if the arrays contain the same values in the
   * same order. Note: Order matters, see the third example
   *
   * compare2IntArrays({3, 4}, {1}) -> false compare2IntArrays({1, 2}, {1, 2}) ->
   * true compare2IntArrays({1, 2}, {2, 1}) -> false compare2IntArrays({1, 2, 3,
   * 4}, {1, 2, 3, 4}) -> true
   **/
   public static boolean compare2IntArrays(int[] firstNumbers, int[] secondNumbers) {
       // your code here

       if (firstNumbers.length == secondNumbers.length) {

           for (int i = 0; i < firstNumbers.length; i++) {

               if (firstNumbers[i] == secondNumbers[i])
                   continue;
               else
                   return false;
           }
       }

       else
           return false;
       return true;
   }// end compare2IntArrays

   /**
   * Given two String arrays return true if the arrays contain the same values in
   * the same order. Note: Order matters, see the forth example Note: Capatil
   * letters matter, see the final example
   *
   * compare2StringArrays({"and"}, {"or"}) -> false compare2StringArrays({"and",
   * "but"}, {"or"}) -> false compare2StringArrays({"a", "b", "c", "d"}, {"a",
   * "b", "c", "d"}) -> true compare2StringArrays({"a", "b", "c", "d"}, {"d", "c",
   * "b", "d"}) -> false compare2StringArrays({"a", "b", "c", "d"}, {"A", "b",
   * "C", "d"}) -> false compare2StringArrays({"Aunt", "Sally"}, {"Aunt",
   * "Sally"}) -> true compare2StringArrays({"Aunt", "Sally"}, {"Aunt", "sally"})
   * -> false
   **/
   public static boolean compare2StringArrays(String[] firstStrings, String[] secondStrings) {
       // your code here
       if (firstStrings.length == secondStrings.length) {

           for (int i = 0; i < firstStrings.length; i++) {

               if (firstStrings[i].compareTo(secondStrings[i]) == 0)
                   continue;
               else
                   return false;
           }
       }

       else
           return false;
       return true;

   }// end TESTcompare2StringArrays

  

}

Feel free to ask any doubts, if you face any difficulty in understanding.

Please upvote the answer if you find it helpful


Add a comment
Know the answer?
Add Answer to:
/** Given an int array, return true if the array contains duplicate values. duplicateInts({3}) -> false...
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
  • /**    Given a String and an array of two Strings,    return a three String...

    /**    Given a String and an array of two Strings,    return a three String array containing the strings in alphabetical order.    Note: Capital letters count    sort3Strings("wallace", {"washington", "irving"}) -> {"irving", "wallace", "washington"}    sort3Strings("wallace", {"washington", "Irving"}) -> {"Irving", "wallace", "washington"}    sort3Strings("Washington", {"irving", wallace"}) -> {"Washington", "irving", "wallace"}    sort3Strings("washington", {"washington", "Washington"}) -> {"Washington", "washington", "washington"} **/ public static String[] sort3Strings(String stringValue, String[] stringArray) {    //your code here    return new String[1]; }//end sort3Strings   ...

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

  • My program wont return boolean. There's nothing in the console.Here's the instructions: Given an array of...

    My program wont return boolean. There's nothing in the console.Here's the instructions: Given an array of ints, compute recursively if the array contains somewhere a value followed in the array by that value times 10. We'll use the convention of considering only the part of the array that begins at the given index.In this way, a recursive call can pass index+1 to move down the array. The initial call will pass in index as 0. examples are: array220([1,2,20],0)----true array220([3,30],0)----true array220([3],0))----false...

  •    /**    * Returns an array of booleans that are set true or    *...

       /**    * Returns an array of booleans that are set true or    * false based on the associated values in the array    * arr using the following rules:    * 1. If arr[i] is divisible by three then the boolean    * value in the the array returned at the same position    * should be true    * 2. Unless the values in arr[i] is also divisible by 5,    * then the value returned...

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

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

  • public static int[] interleaveArray(int[] a, int[] b) Given two arrays, return the array which interleaves the...

    public static int[] interleaveArray(int[] a, int[] b) Given two arrays, return the array which interleaves the elements of the two arrays. The two arrays do not have to be the same length. For example, given a = [1, 2, 3] and b = [4, 5, 6, 7, 8], the method returns c = [1, 4, 2, 5, 3, 6, 7, 8] Parameters: a - given array b - given array Returns: the array which interleaves the elements of the two...

  • Which of the following are valid array declarations? a. int[] array- new int[10]; b. double [array...

    Which of the following are valid array declarations? a. int[] array- new int[10]; b. double [array double[10]; c. charl charArray "Computer Science"; None of the above Analyze the following code: class Test public static void main(Stringl] args) System.out.println(xMethod(10); public static int xMethod(int n) System.out.println("int"); return n; public static long xMethod(long n) System.out.,println("long"); return n The program displays int followed by 10 The program displays long followed by 10. The program does not compile. None of the above. tions 3-4 are...

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