Question

In: Computer Science

numUnique returns the number of unique values the list * Precondition: the list is not empty...

numUnique returns the number of unique values the list * Precondition: the list is not empty and is sorted from low to high. * { your solution can assume this is true } * * For full credit, your solution must go through the list exactly once. * Your solution may not call any other functions. * * Examples: * { abcdef }.numUnique() == 6 * { aaabcd }.numUnique() == 4 * { bccddee }.numUnique() == 4 * { abcddd }.numUnique() == 4 * { a }.numUnique() == 1

FOR JAVA

Solutions

Expert Solution

Main.java

import java.util.*;

public class Main
{
public static int numUnique(List<String> list){
/* First element always be unique so count is set to 1 */
int count=1;
  
/* First element is taken as temp to compare with following
element to find next unique element*/
String temp = list.get(0);
for (int i = 1; i < list.size(); i++) {
if(!temp.equals(list.get(i))){
/*When next unique elememt is found count will be increased
and new element will be set as temp to compare with following element */
count++;
temp = list.get(i);
}
}
return count;
}
  
   public static void main(String[] args) {
List<String> list=new ArrayList<String>();
list.add("a");
list.add("a");
list.add("a");
list.add("b");
list.add("c");
//list.add("c");
list.add("d");
//list.add("d");
//list.add("d");
//list.add("d");
//list.add("e");
//list.add("e");
  
System.out.println(numUnique(list));
   }
}

Code Snippet (For Indentation):

Output


Related Solutions

Create the following java program with class list that outputs: //output List Empty List Empty List...
Create the following java program with class list that outputs: //output List Empty List Empty List Empty Item not found Item not found Item not found Original list Do or do not. There is no try. Sorted Original List Do There do is no not. or try. Front is Do Rear is try. Count is 8 Is There present? true Is Dog present? false List with junk junk Do or moremorejunk do not. There is no try. morejunk Count is...
Write a function which receives a list and returns a number. In the list, all numbers...
Write a function which receives a list and returns a number. In the list, all numbers have been repeated twice except one number that is repeated once. The function should return the number that is repeated once and return it.write a python program for this question. use main function.
Write a recursive methodcalledpermutationthataccepts two integersnandras parameters and returns the number of unique permutations ofritems from...
Write a recursive methodcalledpermutationthataccepts two integersnandras parameters and returns the number of unique permutations ofritems from a group ofnitems. For given values ofnandr, this valuepermutation(n,r)can be computed as follows: permutation(n,r)= n!/ (n-r)! hint: permutation(6, 4)should return360. It may be helpful to note thatpermut(5, 3)returns60, or 360/6.
1) List 7 Values that are important to you and place a number next to the...
1) List 7 Values that are important to you and place a number next to the value indicating which is most important to you such as: 1-Family; 2-Job; 3-Money, etc.( According to the values in Discovery your true north)
In C++, type a method getSmallest(), which returns the smallest number in the following linked list....
In C++, type a method getSmallest(), which returns the smallest number in the following linked list. 8->4->6->7->5 (8 is the head).
Type or paste question here Fill in the empty cells and the values required in the...
Type or paste question here Fill in the empty cells and the values required in the last row of the table. These values will help you answer subsequent questions and calculate the Pearson’s r and the linear regression equation. (2 decimals) X Y ()( 3 3 -2.00 -4.00 4.00 16.00 8.00 6 9 1.00 2.00 1.00 4.00 2.00 5 8 0.00 1.00 0.00 1.00 0.00 4 3 -1.00 -4.00 1.00 16.00 4.00 7 10 2.00 3.00 4.00 9.00 6.00 5...
Question 1: Write a method getSmallest(), which returns the smallest number in the linked list. Question...
Question 1: Write a method getSmallest(), which returns the smallest number in the linked list. Question 2: Write a member method getPosition(int entry) which returns the position of the entry is in the linked list. If the entry is not in the list, return -1. Please use C++ language for both questions, I only need functions.
A airline wishes to estimate the mean number of seats that are empty on flights that...
A airline wishes to estimate the mean number of seats that are empty on flights that use 737-airplanes. There are 189 seats on a plane. To do so, the airline randomly picks n=35 flights. For each flight, the number of empty seats is counted. The data are given below. 38, 42, 44, 42, 40, 45, 37, 31, 33, 36, 35, 39, 37, 37, 43, 38, 41, 27, 33, 35, 37, 46, 32, 35, 35, 42, 37, 41, 29, 40, 44,...
Use Excel to calculate the values to fill in the empty boxes. Feel free to add...
Use Excel to calculate the values to fill in the empty boxes. Feel free to add additional tables and calculations as needed. Please use the assignment 1 discussion board to ask questions. Once completed, save this file and upload it in Canvas. Historical Demand Data 2012 to 2016: The table reproduced below is the demand data for a company (aggregated) for the previous five years. 2012 2013 2014 2015 2016 Q1 11632 15034 16117 15565 16470 Q2 22509 26824 24169...
Use Excel to calculate the values to fill in the empty boxes. Feel free to add...
Use Excel to calculate the values to fill in the empty boxes. Feel free to add additional tables and calculations as needed. Historical Demand Data 2012 to 2016: The table reproduced below is the demand data for a company (aggregated) for the previous five years. 2012 2013 2014 2015 2016 Q1 11632 15034 16117 15565 16470 Q2 22509 26824 24169 20151 42858 Q3 21646 13314 14505 13392 19278 Q4 11355 10698 11176 10613 13934 Annual Demand 67,142 65,870 65,967 59,721...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT