 # JavaScript: Find the number which appears most in a given array of integers

## JavaScript Basic: Exercise-94 with Solution

Write a JavaScript program to find the number which appears most in a given array of integers.

Pictorial Presentation: Sample Solution:

HTML Code:

``````<!DOCTYPE html>
<html>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Find the number which appears most in a given array of integers</title>
<body>

</body>
</html>
```
```

JavaScript Code:

``````function array_element_mode(arr) {
var ctr = [],
ans = 0;

for(var i = 0; i < 10; i++) {
ctr.push(0);
}
for(var i = 0; i < arr.length; i++) {
ctr[arr[i] - 1]++;
if(ctr[arr[i] - 1] > ctr[ans]) {
ans = arr[i] - 1;
}
}
return ans + 1;
}
console.log(array_element_mode([1, 2, 3, 2, 2, 8, 1, 9]))
```
```

Sample Output:

```2
```

Flowchart: ES6 Version:

``````function array_element_mode(arr) {
const ctr = [];
let ans = 0;

for(var i = 0; i < 10; i++) {
ctr.push(0);
}
for(var i = 0; i < arr.length; i++) {
ctr[arr[i] - 1]++;
if(ctr[arr[i] - 1] > ctr[ans]) {
ans = arr[i] - 1;
}
}
return ans + 1;
}
console.log(array_element_mode([1, 2, 3, 2, 2, 8, 1, 9]))
``````

Live Demo:

See the Pen javascript-basic-exercise-94 by w3resource (@w3resource) on CodePen.

What is the difficulty level of this exercise?

﻿

## JavaScript: Tips of the Day

Classes/function constructors

```class Person {
constructor() {
this.name = 'Owen';
}
}

Person = class AnotherPerson {
constructor() {
this.name = 'Eddie';
}
};

const member = new Person();
console.log(member.name);
```

We can set classes equal to other classes/function constructors. In this case, we set Person equal to AnotherPerson. The name on this constructor is Eddie, so the name property on the new Person instance member is "Eddie".

Ref: https://bit.ly/3jFRBje