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)
Write a LISP function COUNTX which takes an atom and a list and returns the number...
Write a LISP function COUNTX which takes an atom and a list and returns the number of top-level occurrences of the atom in the list. For example: (COUNTX ‘A ‘(A (A B) B A B A (B A)) Returns the value 3, the other two A’s are not at the top level
Implement a function that returns the maximum number in a given unsorted linked list. For example,...
Implement a function that returns the maximum number in a given unsorted linked list. For example, there is a linked list 3->5->1->10->9. The printMax() function in max.c should return the maximum number in the linked list, namely 10 in the example. 1. Implement max.c with the completed printMax() function. 2. Provide an explanation for your solution #include <stdio.h> typedef struct node { int value; struct node *next; } node; int printMax(node *first) { // Your implementation return 0; } int...
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).
Add the following methods to the singly list implementation below. int size(); // Returns the number...
Add the following methods to the singly list implementation below. int size(); // Returns the number of nodes in the linked list bool search(string query); // Returns if the query is present in the list void add(List& l); // // Adds elements of input list to front of "this" list (the list that calls the add method) ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- // slist.cpp #include <string> #include "slist.h" using namespace std; Node::Node(string element) : data{element}, next{nullptr} {} List::List() : first{nullptr} {} // Adds to...
Add the following methods to the singly list implementation below. int size(); // Returns the number...
Add the following methods to the singly list implementation below. int size(); // Returns the number of nodes in the linked list bool search(string query); // Returns if the query is present in the list void add(List& l); // // Adds elements of input list to front of "this" list (the list that calls the add method) #include <string> #include "slist.h" using namespace std; Node::Node(string element) : data{element}, next{nullptr} {} List::List() : first{nullptr} {} // Adds to the front of...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT