Question

design an efficient algorithm that, on input a set of n real numbers {x1, . ....

design an efficient algorithm that, on input a set of n real numbers {x1, . . . , xn}, outputs all distinct numbers in the set. For example, if your input is {5, 10, 9, 10, 8, 5, 12, 11, 12, 9, 7, 6, 8, 5}, then you should output {5, 10, 9, 8, 11, 12, 7, 6} (any ordering of these number is acceptable).

Homework Answers

Answer #1

/*

We can Use Hashing to solve this in O(n) time on average. The idea is to traverse the given array from left to right and keep track of visited elements in a hash table. Following is the implementation of the idea.

*/

/* Java program to print all distinct elements of a given array */
import java.util.*;
  
class Main
{
// This function prints all distinct elements
static void printDistinct(int arr[])
{
// Creates an empty hashset
HashSet<Integer> set = new HashSet<>();
  
// Traverse the input array
for (int i=0; i<arr.length; i++)
{
// If not present, then put it in hashtable and print it
if (!set.contains(arr[i]))
{
set.add(arr[i]);
System.out.print(arr[i] + " ");
}
}
}
  
// Driver method to test above method
public static void main (String[] args)
{
int arr[] = {5, 10, 9, 10, 8, 5, 12, 11, 12, 9, 7, 6, 8, 5};
printDistinct(arr);
}
}

Know the answer?
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
Similar Questions
A Queue is a linked list with pointers to both the head of the list as...
A Queue is a linked list with pointers to both the head of the list as well as the tail (or the last element) of the list. Given a queue Q, Q.head gives the head of the queue and Q.tail gives the tail of the queue. Give O(1) time algorithms for the following tasks. Enqueue • Input: A queue Q of distinct integers and a queue element a not in Q. 1 • Output: Q with the element a added...
python If a number num1 divides another number num2 evenly then num1 is a divisor of...
python If a number num1 divides another number num2 evenly then num1 is a divisor of num2. For example, 2 is a divisor of 2, 4, 6, 8, but 2 is not a divisor of 1, 3, 5, 7, 9, 11, 13. Write a function named count_divisors(m,n) that works as follows. Input: the function takes two integer arguments m and n Process: the function asks the user to enter numbers (positive or negative), and counts the total number of inputs...
Divide-and-Conquer technique is famous for providing O(n log n) solutions for problems with O(n2) straight forward...
Divide-and-Conquer technique is famous for providing O(n log n) solutions for problems with O(n2) straight forward solutions. One of those problems is the “Maximum Subarray Sum” or “Maximum Value Contiguous Subsequence": Given a sequence of n numbers A[0 ... n-1], give an algorithm for finding a contiguous subsequence A[i ... j], for which the sum of elements in this subsequence is the maximum we can get by choosing different i or j.   For example: the maximum subarray sum for the...
IN C LANGUAGE This program initially reads two integers from standard input: (1) an integer T...
IN C LANGUAGE This program initially reads two integers from standard input: (1) an integer T and (2) a positive integer N. It then reads N integers and counts the numbers that are greater than T and the numbers than are less than T. It then prints out these two counts. Example What is the threshold value? 7 How many values? 5 Enter 5 values: 6 7 9 9 8 3 values are greater than 7 1 values are less...
write a python program that include a function named activity_selection() and take in two arguments, first...
write a python program that include a function named activity_selection() and take in two arguments, first one would be the number of tasks and the second argument would be a list of activities. Each activity would have an activity number, start time and finish time. Example activity_selection input and output: activity_selection (11, [[1, 1, 4 ], [2, 3, 5], [3, 0, 6], [4, 5, 7], [5, 3, 9], [6, 5, 9],[7, 6, 10], [ 8, 8, 11], [ 9, 8,...
Pinky and The Brain are great friends. They like to play games with numbers. This time,...
Pinky and The Brain are great friends. They like to play games with numbers. This time, Pinky has given The Brain a list of numbers and given him the task of determining if it is possible to choose a subset of them such that they sum is equal to another given number. Build an algorithm using dynamic programming to help The Brain with his problem. INPUT The first line corresponds to N, the amount of numbers given by Pinky The...
I need a breakdown to perform in excel for numbers 7,8,9. I am unsure of how...
I need a breakdown to perform in excel for numbers 7,8,9. I am unsure of how I calculate the times. heres the data set and the questions. Calculate the probability that a flight will depart early or on time. Calculate the probability that a flight will arrive late. Calculate the probability that a flight departs late or arrives early. DEP_Delay ARR_Delay -4 0 -3 -3 0 -5 -7 -1 8 3 -1 -5 3 8 11 6 -6 0 -5...
by MULTISIM Design a 4 bit Counter that displays even numbers when a switch on, and...
by MULTISIM Design a 4 bit Counter that displays even numbers when a switch on, and odd when the switch off . i want you to desgin that cirucit in MULTIsim by useing Jk flip flop please make it easy to understand and memories =[ that mean if it was even= 0 its will count 0 , 2 , 4 ,6 , 8 , 10 , 14 if it is odd = 1 its will count 1 , 3 ,...
Consider the experimental results for the following randomized block design. Make the calculations necessary to set...
Consider the experimental results for the following randomized block design. Make the calculations necessary to set up the analysis of variance table. Treatment A B C 1 11 10 9 Blocks 2 12 6 6 3 18 16 14 4 21 18 19 5 8 8 9 Use = .05 to test for any significant differences. Show entries to 2 decimals, if necessary. Round p-value to four decimal places. If your answer is zero enter "0". Source of Variation Sum...
Please follow the above definitions in calculating the quartiles. There are two distinct situations: set size...
Please follow the above definitions in calculating the quartiles. There are two distinct situations: set size equal to a power of 4 (L is already a whole number – integer), set size not a power of 4. Note: each of the following problems is 15 points. PROBLEM 2: Average wait in minutes for the train QM3; n=12 15 4 5 9 6 12 17 10 8 13 8 16 PROBLEM 3: Quiz scores L n = 13 students 5   7  ...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT