Help me in Task Completion

Perform linear search operation on an array
And given array [1, 1, 2, 3, 4, 4, 4, 5]
Put these in a map
Map should finally look like

{
1: 2,
2: 1,
3: 1,
4: 3,
5: 1
}

Key value pairs
Key is array element
Value is frequency of element in array

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

That would be done with either a for loop or the Array’s .forEach method. Pseudo-code with a for loop could look like this:

let map = {};

for (let i=0; i<arr.length; i++){

  // if arr[i] is a property(key) on the map object, increase that value by 1

  // else, create that property (key) on the map object and set it to 1

}
return map;