Question

JAVA Help Please. Thank you! /**    * deDupAndReverse returns a new array containing the unique...

JAVA Help Please. Thank you!

/**
   * deDupAndReverse returns a new array containing the unique values in the
   * original array reversed. There should not be any extra space in the array --- there should
   * be exactly one space for each unique element (Hint: numUnique tells you
   * how big the array should be). You may assume that the list is sorted, as
   * you did for numUnique.
   *
   * Your solution may call numUnique, but should not call any other
   * functions. After the call to numUnique, you must go through the array
   * exactly one more time. Here are some examples (using "==" informally):
   *
   *   Precondition: the array may be empty, but if it is not empty the array is sorted from low to high.
   * { your solution can assume this is true }
   *
   * <pre>
   * new int[] { } == deDupAndReverse(new int[] { })
   * new int[] { 11 } == deDupAndReverse(new int[] { 11 })
   * new int[] { 11 } == deDupAndReverse(new int[] { 11, 11, 11, 11 })
   * new int[] { 88, 77, 66, 55, 44, 33, 22, 11 } == deDupAndReverse(new int[] { 11, 11, 11, 11, 22, 33, 44, 44, 44, 44, 44, 55, 55, 66, 77, 88, 88 })
   * new int[] { 88, 77, 66, 55, 44, 33, 22, 11 } == deDupAndReverse(new int[] { 11, 22, 33, 44, 44, 44, 44, 44, 55, 55, 66, 77, 88 })
   * </pre>
   */
   public static int[] deDupAndReverse (int[] list) {
      
       return list; // TODO2: fix this
   }

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public static int[] deDupAndReverse (int[] list) {
    int tmp[] = new int[list.length];
    int size = 0;
    boolean found;
    for(int i = list.length-1;i>=0;i--){
        found = false;
        for(int j = 0;j<size;j++){
            if(list[i]==tmp[j]){
                found=true;
            }
        }
        if(found){
            tmp[size++] = list[i];
        }
    }
    int result[] = new int[size];
    for(int i = 0;i<size;i++){
        result[i] = tmp[i];
    }
    return result; 
}
Add a comment
Know the answer?
Add Answer to:
JAVA Help Please. Thank you! /**    * deDupAndReverse returns a new array containing the unique...
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
  • Another simple sort is the odd-even sort. The idea is to repeatedly make two passes through the a...

    Another simple sort is the odd-even sort. The idea is to repeatedly make two passes through the array. On the first pass you look at all the pairs of items, a[j] and a[j+1], where j is odd (j = 1, 3, 5, …). If their key values are out of order, you swap them. On the second pass you do the same for all the even values (j = 2, 4, 6, …). You do these two passes repeatedly until...

  • package exampleassignment; import java.util.Arrays; import stdlib.*; public class assignment {        /**        * valRange returns...

    package exampleassignment; import java.util.Arrays; import stdlib.*; public class assignment {        /**        * valRange returns the difference between the maximum and minimum values in the        * array; Max-Min. Precondition: the array is nonempty. Your solution must go        * through the array at most once.        *        * Here are some examples (using "==" informally):        *        * <pre>        *    0 == valRange (new double[] { -7 })        *    10 == valRange (new double[]...

  • Add a method called median() to the ArrayIns class in the insertSort.java program (Listing 3.3). This...

    Add a method called median() to the ArrayIns class in the insertSort.java program (Listing 3.3). This method should return the median value in the array. (Recall that in a group of numbers half are larger than the median and half are smaller.) Do it the easy way. LISTING 3.3 The insertSort.java Program // insertSort.java // demonstrates insertion sort // to run this program: C>java InsertSortApp //-------------------------------------------------------------- class ArrayIns { private long[] a; // ref to array a private int nElems;...

  • 1. Create a new class called ReversibleArray. 2. In the class header, add the code <T>...

    1. Create a new class called ReversibleArray. 2. In the class header, add the code <T> immediately to the right of the class name. 3. Give this class two private instance variables: T[] array and int count 4. Create a constructor which takes in one parameter of type T[] and assign that parameter's value into this.array. Set count as the length of the array. 5. Add a toString() method that outputs the array values in the format: elem0, elem1, elem2,...

  • PYTHON please help, stuck on this output and memory image! THANK YOU! Q3 7 Points import...

    PYTHON please help, stuck on this output and memory image! THANK YOU! Q3 7 Points import copy list1 = [11, [22, 33], [44, 55], 66] list2 = list1 list3 = listi[:] list4 = copy.deepcopy(listi) list1[0] = 100 list1[1][1] = 300 list1 += [77] list2 = list2 + [88] print(listi) print(list2) print(list) print(list) Please write output of the above program below (in the text box) and upload a picture of memory image? Enter your answer here Memory Image: Please select file(s)...

  • The ExceptionLab class provided: – Creates an array of 100 elements and fills it with random...

    The ExceptionLab class provided: – Creates an array of 100 elements and fills it with random numbers from 1 to 100. – It asks the user for an index value between 0 and 99. – Prints the element at that position. – If a number > 99 is entered by the user, the class will abort with an ArrayIndexOutOfBoundsException • Modify the ExceptionLab: – Add a try-catch clause which intercepts the ArrayIndexOutOfBounds and prints the message: Index value cannot be...

  • Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges...

    Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges two arrays of positive integers and removes any duplicate entries. Your program will first ask for a valid length which must be an integer which is 10 or greater. The program should continue to ask until a valid length is entered. The program will then create two arrays of the length entered, fill these with random integers between 1 and 100 inclusive, and print...

  • To the HighArray class in the highArray.java, add a method called getMax() that returns the value...

    To the HighArray class in the highArray.java, add a method called getMax() that returns the value of the highest key in the array, or -1 if the array is empty. Add some code in main() to exercise this method. You can assume all the keys are positive numbers. // highArray.java class HighArray { private long[] a; private int nElems;    public HighArray(int max)    { a = new long[max];    nElems = 0; } public boolean find(long searchKey) { int...

  • Fix all sections marked TO DO. Must show output. package JavaExcercise; import java.util.Arrays; import stdlib.*; /**...

    Fix all sections marked TO DO. Must show output. package JavaExcercise; import java.util.Arrays; import stdlib.*; /** * Do not change the declaration of any method. * Each of the functions below is meant to be SELF CONTAINED. This means that * you should use no other functions or classes unless otherwise indicated. * You should not use any HashSets or ArrayLists, or anything else. * In addition, each of your functions should go * through the argument array at most...

  • Python 3 Create a function that takes an array and an integer v, and returns the...

    Python 3 Create a function that takes an array and an integer v, and returns the number of v occurrences in the array. Add a variable Nsteps to count how many times the loops have executed and display that information in a message The main program must take an array / list 2D, call the function, and display the result. >>>l3 = [52, 14, 14, 8, 85, 69, 1, 77, 94, 96, 51, 65, 35, 32, 87, 92, 74, 47,...

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