
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Unique number of occurrences of elements in an array in JavaScript
We are required to write a JavaScript function that takes in an array of integers as the first and the only argument.
The function should whether all the integers that are present in the array appear for unique number of times or not.
If they do, the function should return true, false otherwise.
For example −
If the input array is −
const arr = [7, 5, 5, 8, 2, 4, 7];
Then the output should be −
const output = false;
because both the integers 7 and 5 appears for 2 times each.
We will first use a hash map to map integers to their frequencies(occurrences) and then use that map to build a set that stores unique frequencies.
Example
Following is the code −
const arr = [7, 5, 5, 8, 2, 4, 7]; const uniqueAppearances = (arr = []) => { const map = {}; const set = new Set(); for(let i = 0; i < arr.length; i++){ const el = arr[i]; map[el] = (map[el] || 0) + 1; }; for(key in map){ const value = map[key]; if(set.has(value)){ return false; }; set.add(value); }; return true; }; console.log(uniqueAppearances(arr));
Output
Following is the console output −
false
- Related Articles
- Unique Number of Occurrences in Python
- JavaScript Count the number of unique elements in an array of objects by an object property?
- Counting unique elements in an array in JavaScript
- How to count number of occurrences of repeated names in an array - JavaScript?
- Sorting array of exactly three unique repeating elements in JavaScript
- Counting the occurrences of JavaScript array elements and put in a new 2d array
- Count occurrences of the average of array elements with a given number in C++
- How to count the number of occurrences of all unique values in an R data frame?
- Iterating through an array, adding occurrences of a true in JavaScript
- Count unique elements in array without sorting JavaScript
- Return an array with the number of nonoverlapping occurrences of substring in Python
- Rearranging elements of an array in JavaScript
- Remove all occurrences of a multiple occurring element in an array in JavaScript
- Finding unique string in an array in JavaScript
- Sum of distinct elements of an array in JavaScript

Advertisements