In: Computer Science
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?
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.