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...
Add a CountGroups method to the linked list class below (OurList). It returns the number of...
Add a CountGroups method to the linked list class below (OurList). It returns the number of groups of a value from the list. The value is passed into the method. A group is one or more values. Examples using strings: A list contains the following strings: one, one, dog, dog, one, one, one, dog, dog, dog, dog, one, one, dog, one    CountGroup(“one”) prints 4 groups of one's    CountGroup(“dog”) prints 3 groups of dog's Do not turn in the...
Add a CountGroups method to the linked list class below (OurList). It returns the number of...
Add a CountGroups method to the linked list class below (OurList). It returns the number of groups of a value from the list. The value is passed into the method. A group is one or more values. Examples using strings: A list contains the following strings: one, one, dog, dog, one, one, one, dog, dog, dog, dog, one, one, dog, one    CountGroup(“one”) prints 4 groups of one's    CountGroup(“dog”) prints 3 groups of dog's Do not turn in the...
Code in C++ Assign negativeCntr with the number of negative values in the linked list. #include...
Code in C++ Assign negativeCntr with the number of negative values in the linked list. #include <iostream> #include <cstdlib> using namespace std; class IntNode { public: IntNode(int dataInit = 0, IntNode* nextLoc = nullptr); void InsertAfter(IntNode* nodePtr); IntNode* GetNext(); int GetDataVal(); private: int dataVal; IntNode* nextNodePtr; }; // Constructor IntNode::IntNode(int dataInit, IntNode* nextLoc) { this->dataVal = dataInit; this->nextNodePtr = nextLoc; } /* Insert node after this node. * Before: this -- next * After: this -- node -- next */...
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).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT