Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java - numUnique returns the number of unique values in an array of doubles. Unl

ID: 3686060 • Letter: J

Question

Java - numUnique returns the number of unique values in an array of doubles. Unlike the previous questions, the array may be empty and it may contain duplicate values. Also unlike the previous questions, you can assume the array is sorted. Your solution must go through the array exactly once. Your solution must not call any other functions. Here are some examples (using "==" informally):

<pre>

0 == numUnique(new double[] { })

1 == numUnique(new double[] { 11 })

1 == numUnique(new double[] { 11, 11, 11, 11 })

8 == numUnique(new double[] { 11, 11, 11, 11, 22, 33, 44, 44, 44, 44, 44, 55, 55, 66, 77, 88, 88 })

8 == numUnique(new double[] { 11, 22, 33, 44, 44, 44, 44, 44, 55, 55, 66, 77, 88 })

</pre>

   public static int numUnique (double[] list) {

       return 0; // TODO

   }

Explanation / Answer


public class Temp1 {
   public static int numUnique (double[] list) {
       int count=0;
       if(list.length==0)return 0;
       for(int i=0;i<list.length-1;i++)
       {
           if(list[i]<list[i+1])count++;
       }
return count+1; // TODO
}
   public static void main(String args[]) {
       System.out.println(numUnique(new double[] { }));
       System.out.println(numUnique(new double[] { 11}));
       System.out.println(numUnique(new double[] { 11, 11, 11, 11 }));
       System.out.println(numUnique(new double[] { 11, 11, 11, 11, 22, 33, 44, 44, 44, 44, 44, 55, 55, 66, 77, 88, 88}));
       System.out.println(numUnique(new double[] { 11, 22, 33, 44, 44, 44, 44, 44, 55, 55, 66, 77, 88 }));
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote