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

Using Java, Given an array A[0 ... n-1], where each element of the array represent a...
Using Java, Given an array A[0 ... n-1], where each element of the array represent a vote in the election. Assume that each vote is given as an integer representing the ID of the chosen candidate. Can you determine who wins the election? What is the complexity of your solution? Hint: it is similar to finding the element that is repeated the maximum number of times.
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[0 … n-1], where each element of the array represent a vote in...
Given an array A[0 … n-1], where each element of the array represent a vote in the election. Assume that each vote is given as an integer representing the ID of the chosen candidate. Can you determine who wins the election? What is the complexity of your solution? Hint: it is similar to finding the element that is repeated the maximum number of times.
In Java, write a program that given an array A[1...n] and a value S finds 0...
In Java, write a program that given an array A[1...n] and a value S finds 0 < i < j < n such that A[i] + A[j] = S
[JAVA SCRIPT] Please create an array of student names and another array of student grades. Create...
[JAVA SCRIPT] Please create an array of student names and another array of student grades. Create a function that can put a name and a grade to the arrays. Keep Read student name and grade until student name is “???”. And save the reading by using a function Create another function to show all the grade in that object. Create the third function that can display the maximum grade and the student’s name. Create a sorting function that can sort...
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.
Please use Java eclipse Find pair in an array with given sum Given an array of...
Please use Java eclipse Find pair in an array with given sum Given an array of integers A and an integer S, determines whether there exist two elements in the array whose sum is exactly equal to S or not. Display 1 a pair is found in an array with matching sum S else 0. Input     6     5     1 -2 3 8 7     Where, First line represents integer S. Second line represents the size of an array. Third line represents...
IN java Create a New Java Project called LastNameDuplicate. //Given an array of N elements with...
IN java Create a New Java Project called LastNameDuplicate. //Given an array of N elements with each element between 1 and N, write a program to determine whether there are any duplicates. //You must prompt the user for the array elements. //Display the contents of the array, along with the values that are duplicated and how many times they appeared in the array. //NOTE: N should be at least 15. Input Validation: Verify that each element entered has a value...
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
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT