Question

In: Computer Science

Need these written in Java script please Problem 1: Given an array A[0 ... n-1], where...

Need these written in Java script please

Problem 1:

Given an array A[0 ... n-1], where each element of the array represents a vote in the election. Assume that each vote is given as integers representing the ID of the chosen candidate. Write the code determining who wins the election.

Problem 2:

How do we find the number which appeared maximum number of times in an array?

Solutions

Expert Solution

1.

Code:

<!DOCTYPE html>
<html>
<head>
   <!-- character set-->
   <meta charset=utf-8 />
   <title>
       Election Results
   </title>
  
</head>
<body>
   <script type="text/javascript">
       //candidate 1 id is 1
       //candidate 2 id is 2
       //candidare 3 id is 3
       //candidate 4 id is 4
       //lets assume the given array
       var arr=[1,1,2,2,1,4,3,3,4,1,2,3,2,4,3,4,2,2,2,2,2];
       //the result
       var res;
       //maximum number of tmes a candidate occured
       var max_num=1;
       //count if each candidate
       var count=0;

       //here we traverse each element in the first loop
       //in the second loop we compare that element with every other
       //element in the array and if they are equal then count is incremented
       //every time we compare count with max_num
       //if max_num is lessthan count max num is set to count
       //and the result is stored
       for (var i = 0; i < arr.length; i++) {
           for (var j = i; j < arr.length; j++) {
               if(arr[i] == arr[j]){
                   count++;
               }
               if(max_num<count){
                   max_num=count;
                   res=arr[i];
                  
               }
              
           }
           //count is set to 0 for every iteration
           count=0;
  
       }
       //result
       if(res==1){
           document.write("The Winning Candidate is Candidate 1");
       }
       else if(res==2){
           document.write("The Winning Candidate is Candidate 2");
       }
       else if(res==3){
           document.write("The Winning Candidate is Candidate 3");
       }
       else{
           document.write("The Winning Candidate is Candidate 4");
       }
   </script>
</body>
</html>

output:

Code Screenshot:

Code Snippet:

<!DOCTYPE html>
<html>
<head>
        <!-- character set-->
        <meta charset=utf-8 />
        <title>
                Election Results
        </title>
        
</head>
<body>
        <script type="text/javascript">
                //candidate 1 id is 1
                //candidate 2 id is 2
                //candidare 3 id is 3
                //candidate 4 id is 4
                //lets assume the given array 
                var arr=[1,1,2,2,1,4,3,3,4,1,2,3,2,4,3,4,2,2,2,2,2];
                //the result
                var res;
                //maximum number of tmes a candidate occured
                var max_num=1;
                //count if each candidate
                var count=0;

                //here we traverse each element in the first loop
                //in the second loop we compare that element with every other
                //element in the array and if they are equal then count is incremented
                //every time we compare count with max_num
                //if max_num is lessthan count max num is set to count
                //and the result is stored
                for (var i = 0; i < arr.length; i++) {
                        for (var j = i; j < arr.length; j++) {
                                if(arr[i] == arr[j]){
                                        count++;
                                }
                                if(max_num<count){
                                        max_num=count;
                                        res=arr[i];
                                        
                                }
                                
                        }
                        //count is set to 0 for every iteration
                        count=0;
        
                }
                //result
                if(res==1){
                        document.write("The Winning Candidate is Candidate 1");
                }
                else if(res==2){
                        document.write("The Winning Candidate is Candidate 2");
                }
                else if(res==3){
                        document.write("The Winning Candidate is Candidate 3");
                }
                else{
                        document.write("The Winning Candidate is Candidate 4");
                }
        </script>
</body>
</html>

The code for second question is same as 1 st question

in both cases we are finding an element which occured maximum number of times.

The explanation is given below

2.

Code:

<!DOCTYPE html>
<html>
<head>
   <!-- character set-->
   <meta charset=utf-8 />
   <title>
       Problem 2
   </title>
  
</head>
<body>
   <script type="text/javascript">
      
       var arr=[1,7,4,3,9,3,4,8,1,6,9,8,6,4,3,2];
       //the result
       var res;
       //maximum number of times an element occured
       var max_num=1;
       //count of each element
       var count=0;

       //here we traverse each element in the first loop
       //in the second loop we compare that element with every other
       //element in the array and if they are equal then count is incremented
       //every time we compare count with max_num
       //if max_num is lessthan count max num is set to count
       //and the result is stored
       for (var i = 0; i < arr.length; i++) {
           for (var j = i; j < arr.length; j++) {
               if(arr[i] == arr[j]){
                   count++;
               }
               if(max_num<count){
                   max_num=count;
                   res=arr[i];
                  
               }
              
           }
           //count is set to 0 for every iteration
           count=0;
  
       }
       //result
       document.write("The Element that occured most times is "+res);
   </script>
</body>
</html>

Output:

Code Screenshot:

Code Snippet:

<!DOCTYPE html>
<html>
<head>
        <!-- character set-->
        <meta charset=utf-8 />
        <title>
                Problem 2
        </title>
        
</head>
<body>
        <script type="text/javascript">
                 
                var arr=[1,7,4,3,9,3,4,8,1,6,9,8,6,4,3,2];
                //the result
                var res;
                //maximum number of times an element occured
                var max_num=1;
                //count of each element
                var count=0;

                //here we traverse each element in the first loop
                //in the second loop we compare that element with every other
                //element in the array and if they are equal then count is incremented
                //every time we compare count with max_num
                //if max_num is lessthan count max num is set to count
                //and the result is stored
                for (var i = 0; i < arr.length; i++) {
                        for (var j = i; j < arr.length; j++) {
                                if(arr[i] == arr[j]){
                                        count++;
                                }
                                if(max_num<count){
                                        max_num=count;
                                        res=arr[i];
                                        
                                }
                                
                        }
                        //count is set to 0 for every iteration
                        count=0;
        
                }
                //result
                document.write("The Element that occured most times is "+res);
        </script>
</body>
</html>

Explanation:

To find the lement that occured most times in an array , first we need to use two loops. the outer loop traverses each element starting from index 1 and in the inner loop we compare that element with every other element and every time it gets an elment which is equal it increments the count. Now every time we update the max frequency by cmparing it with count. And in the end we get the element that occured maximum and its frequency.


Related Solutions

Problem 1: Given an array A[0 ... n-1], where each element of the array represents a...
Problem 1: Given an array A[0 ... n-1], where each element of the array represents a vote in the election. Assume that each vote is given as integers representing the ID of the chosen candidate. Write the code determining who wins the election. Problem 2: How do we find the number which appeared maximum number of times in an array? ( Use Java and an original code )
Given an array A[1..n] of integers - all of whose numbers are in the range [0,...
Given an array A[1..n] of integers - all of whose numbers are in the range [0, n^3 − 1] give an algorithm which sorts them in O(n) time.
Write a program in Java with a Scanner. Given an array and a number k where...
Write a program in Java with a Scanner. Given an array and a number k where k is smaller than the size of the array, write a program to find the k'th smallest element in the given array. It is given that all array elements are distinct. Example: Input: arr[] = {7,10,4,3,20,15} k = 3 Output: 7
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array and remove the duplicates in-place such that each element appears only once in the input array and returns the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Find the time complexity of your removeDuplicates() method in Big-O notation and write that in a comment line on the top...
java Factorials The factorial of n (written n!) is the product of the integers between 1...
java Factorials The factorial of n (written n!) is the product of the integers between 1 and n. Thus 4! = 1*2*3*4 = 24. By definition, 0! = 1. Factorial is not defined for negative numbers. Write a program that asks the user for a non-negative integer and computes and prints the factorial of that integer. You’ll need a while loop to do most of the work—this is a lot like computing a sum, but it’s a product instead. And...
Array with Pointers Find Continuous Sub-Array C++ Problem: Given an unsorted array A of size N...
Array with Pointers Find Continuous Sub-Array C++ Problem: Given an unsorted array A of size N of non-negative integers, find a continuous sub-array which adds to the given number. Declare dynamic arrays and use only pointers syntax (no [ ]’s or (ptr+i) stuff.     Input will be the number of input values to enter followed by the sum to compare with. Print out the continuous sub-array of values that are equal to sum or the message ‘No sum found’. There...
Write a java script function that accepts integer array as input, and display a new array...
Write a java script function that accepts integer array as input, and display a new array by performing fol lowing modifications, • The values in odd index positions must be incremented by 1 • The values in even index positions must be decremented by 1. • Assume the array index starts from 0. Input 1: arr = [1,2,3,4] Output 1: arr = [0,3,2,5 it done html and javascript and plz do it in simple way
for an array A of size N, and A[0] != A[n-1]. devise an efficient algorithm for...
for an array A of size N, and A[0] != A[n-1]. devise an efficient algorithm for find a pair of elements A[i] and A[i+1] such that A[i] != A[i+1]. can you always find such pair and why
please write in c++ Algorithm Design problem: Counting inversions: given an array of n integers, find...
please write in c++ Algorithm Design problem: Counting inversions: given an array of n integers, find out the total number of inversions in the given sequence. For example, for the sequence of 2, 4, 1, 3, 5, there are three inversions (2,1), (4,1), and (4,3). Give a brute-force algorithm with running time of O(n^2). Using the technique of divide-and-conquer, design an algorithm with running time of O(nlog n).
give an algorithm where given as input an array A[1...n] withthe following property. There exists...
give an algorithm where given as input an array A[1...n] with the following property. There exists an index I such that if we append A[1...i−1] after A[i...n], we get an array in sorted increasing order. For simplicity you can assume that n is a power of 2.Give an efficient algorithm that returns the smallest element in A. Analyze the time complexity of your algorithm.Hint: you may want to compare A[1] and A[n/2].
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT