Question

Write a method List-Double> duplicates(List<Double> input) that returns a List of all duplicates in the input List. You may a
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Since you have not mentioned the language of your preference, i am providing the code in Java.

Please find the code below.

CODE

public List<Double> duplicates (List<Double> input) {

Map<Double, Integer> map = new HashMap<>();

for (Double inp : input) {

if (map.containsKey(inp)) {

map.put(inp, map.get(inp) + 1);

} else {

map.put(inp, 1);

}

}

List<Double> res = new ArrayList<>();

for (Map.Entry<Double, Integer> en : map.entrySet()) {

if (en.getValue() > 1) {

res.add(en.getKey());

}

}

return res;

}

Add a comment
Know the answer?
Add Answer to:
Write a method List-Double> duplicates(List<Double> input) that returns a List of all duplicates in the input...
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
  • Write a method List duplicates(List-Doubles input) that returns a List af all duplicates in the i...

    Write a method List duplicates(List-Doubles input) that returns a List af all duplicates in the input List. You may assume thai each elemert occurs either once or twicc. The expected runtime nust be Oo) whece n is the number a elements (even if the input is a Linkodl ist, the runtime should still he n) ino credit for slower runtimes, so you cancsot sort the input). (12 point) Examples: input 13, 4,4 retum [4 input 13.7, 4, 7,3] rctum 17,...

  • Need help to answer this method in java Write a method int indexFirstOne(int[ ] input) The...

    Need help to answer this method in java Write a method int indexFirstOne(int[ ] input) The input array is sorted, and every element is 0 or 1. Return the index of the first 1. If there are no 1s in the array, return -1. The worst-case runtime must be O(logn)where n is the number of elements (no credits for slower runtimes) Example: a = [0,0,1,1,1]      return 2 a = [ 0,0,0,1]          return 3 a = [0,0,0]              return -1 int indexFirstOne...

  • JAVA - Write a recursive function that takes a linked list as input and returns all...

    JAVA - Write a recursive function that takes a linked list as input and returns all of the elements in the linked list. Input: 1, 2, 3, 4, 5 Output: (1+2+3+4+5)/5 = 3 What I have: public static int findMean (Node head, Node cur, int n){ int average = 0; if (head == null){ if(n == 0) return 0; } if (n > 0){ int sum = n + findMean(head, head.next, n -1); average = (sum)/n; } return average; }...

  • DO NOT CHANGE THE METHOD HEADER Write a method int indexFirstOne (int [] input) The input...

    DO NOT CHANGE THE METHOD HEADER Write a method int indexFirstOne (int [] input) The input array is sorted and every element is 0 or 1. Return the index of the first 1. If there are no 1s in the array, return -1. The worst case runtime must be O(logn) where n is the number of elements. Example a = [0, 0, 1, 1, 1] return 2

  • Consider the following problem: Input: a list of n-1 integers and these integers are in the...

    Consider the following problem: Input: a list of n-1 integers and these integers are in the range of 1 to n. There are no duplicates in list. One of the integers from 1 to n is missing in the list. Output: find the missing integer Let the input array be [2, 4, 1, 6, 3, 7, 8]. Elements in this list are in the range of 1 to 8. There are no duplicates, and 5 is missing. Your algorithm needs...

  • Problem 1 Given a linked list of integers, write a function getUnique that removes all duplicates elements in the linked list and returns the new linked list of unique elements (The order does not ma...

    Problem 1 Given a linked list of integers, write a function getUnique that removes all duplicates elements in the linked list and returns the new linked list of unique elements (The order does not matter). Example: Input: 1-»2->3->1-2 Output: 1->2->3 public class Node f int iterm Node next; Node(int d) t item = d; next-null; ) import java.util.ArrayList; public class ExtraLab public static void main (String[] args)t PROBLEM 1 System.out.println("PROBLEM 1"); Node head new Node(1); head.next-new Node (2); head.next.next-new Node(3);...

  • Q3: Write the method that returns true if all the elements of the linked list are...

    Q3: Write the method that returns true if all the elements of the linked list are different. It return false otherwise (in java + the output pic )

  • java Create the following classes: DatabaseType: an interface that contains one method 1. Comparator getComparatorByTrait(String trait)...

    java Create the following classes: DatabaseType: an interface that contains one method 1. Comparator getComparatorByTrait(String trait) where Comparator is an interface in java.util. Database: a class that limits the types it can store to DatabaseTypes. The database will store the data in nodes, just like a linked list. The database will also let the user create an index for the database. An index is a sorted array (or in our case, a sorted ArrayList) of the data so that searches...

  • (Remove duplicates) Write a method that removes the duplicate elements from an array list of integers...

    (Remove duplicates) Write a method that removes the duplicate elements from an array list of integers using the following header: public static void removeDuplicate(ArrayList < Integer > list) Write a test program that prompts the user to enter 10 integers to a list and displays the distinct integers in their input order separated by exactly one space. Sample Run 1 Enter ten integers: 34 5 3 5 6 4 33 2 2 4 The distinct integers are 34 5 3...

  • Write a method max() which take a generic singly linked list as an argument and returns...

    Write a method max() which take a generic singly linked list as an argument and returns the maximum element reference in the list.      If the list is empty, please throw an empty collection exception.      The method prototype is defined as follows. Please write down your code in the method body enclosed in braces: public static <T extends Comparable<T>> T max(SingleLinkedList<T> list) throws EmptyCollectionException { } 2) What is the big O notation of the max method? a) O(N)...

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