
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Is element repeated more than n times in JavaScript
We are required to write a JavaScript function that takes in two arguments −
An Array, say arr, of literals that may contain some repeating elements.
A number, say limit.
The function should validate that no element of the array is repeated more than limit number of times. If any element is repeated more than the limit the function should return false, true otherwise.
Therefore, let’s write the code for this function −
Example
The code for this will be −
const arr = [4, 6, 7, 4, 2, 5, 7, 7, 4, 4, 3]; const validateElements = (arr, n) => { const counts = arr.reduce((acc, el) => { acc[el] = (acc[el] + 1) || 1; return acc; }, {}); return Object.values(counts).every(c => { return c < n; }); }; console.log(validateElements(arr, 3)); console.log(validateElements(arr, 4)); console.log(validateElements(arr, 6));
Output
The output in the console will be −
false false true
- Related Questions & Answers
- Deleting occurrences of an element if it occurs more than n times using JavaScript
- JavaScript array: Find all elements that appear more than n times
- How to return a string repeated N number of times in C#?
- Is Microsoft more successful than Apple?
- The n times dribbling strings in JavaScript
- Element Appearing More Than 25% In Sorted Array in C++
- Limiting elements occurrences to n times in JavaScript
- Program to check pattern of length m repeated K or more times exists or not in Python
- Using more than one CSS classes for an element in HTML
- Insert more than one element at once in a C# List
- How to validate if an element in an array is repeated? - JavaScript
- Subset groups that occur greater than equal to n times in R dataframe.
- Find the most frequent number in the array and how many times it is repeated in JavaScript
- Find maximum N such that the sum of square of first N natural numbers is not more than X in C++
- Find maximum N such that the sum of square of first N natural numbers is not more than X in Python
Advertisements