Skip to content

counting

Count the occurrences of each element in an array.

Syntax

typescript
counting<T>(
  array: T[],
  key?: (item: T) => any
): Record<string, number>

Parameters

  • array (T[]): The array to count
  • key ((item: T) => any, optional): Key extraction function for counting

Return Value

  • Record<string, number>: Counting result object, keys are element values, values are occurrence counts

Examples

Basic Usage

typescript
import { counting } from 'radash'

const fruits = ['apple', 'banana', 'apple', 'cherry', 'banana', 'apple']
const counts = counting(fruits)
// { 'apple': 3, 'banana': 2, 'cherry': 1 }

Counting Object Arrays

typescript
import { counting } from 'radash'

const users = [
  { name: 'Alice', city: 'New York' },
  { name: 'Bob', city: 'Los Angeles' },
  { name: 'Charlie', city: 'New York' },
  { name: 'Diana', city: 'Los Angeles' },
  { name: 'Eve', city: 'New York' }
]

const cityCounts = counting(users, user => user.city)
// { 'New York': 3, 'Los Angeles': 2 }

Counting Number Arrays

typescript
import { counting } from 'radash'

const scores = [85, 92, 78, 85, 90, 92, 85]
const scoreCounts = counting(scores)
// { '85': 3, '92': 2, '78': 1, '90': 1 }

Counting Nested Properties

typescript
import { counting } from 'radash'

const orders = [
  { id: 1, customer: { name: 'Alice', tier: 'Gold' } },
  { id: 2, customer: { name: 'Bob', tier: 'Silver' } },
  { id: 3, customer: { name: 'Charlie', tier: 'Gold' } },
  { id: 4, customer: { name: 'Diana', tier: 'Bronze' } }
]

const tierCounts = counting(orders, order => order.customer.tier)
// { 'Gold': 2, 'Silver': 1, 'Bronze': 1 }

Counting Conditional Results

typescript
import { counting } from 'radash'

const products = [
  { name: 'Laptop', price: 999 },
  { name: 'Phone', price: 599 },
  { name: 'Book', price: 29 },
  { name: 'Tablet', price: 399 },
  { name: 'Watch', price: 199 }
]

const priceRangeCounts = counting(products, product => {
  if (product.price < 100) return 'Budget'
  if (product.price < 500) return 'Mid-range'
  return 'Premium'
})
// { 'Budget': 1, 'Mid-range': 2, 'Premium': 2 }

Counting Boolean Values

typescript
import { counting } from 'radash'

const responses = [true, false, true, true, false, true]
const responseCounts = counting(responses)
// { 'true': 4, 'false': 2 }

Handling Null Values

typescript
import { counting } from 'radash'

const items = [
  { name: 'Item 1', category: 'A' },
  { name: 'Item 2', category: null },
  { name: 'Item 3', category: 'B' },
  { name: 'Item 4', category: undefined },
  { name: 'Item 5', category: 'A' }
]

const categoryCounts = counting(items, item => item.category || 'Uncategorized')
// { 'A': 2, 'B': 1, 'Uncategorized': 2 }

Counting String Characters

typescript
import { counting } from 'radash'

const text = 'hello world'
const charCounts = counting(text.split(''))
// { 'h': 1, 'e': 1, 'l': 3, 'o': 2, ' ': 1, 'w': 1, 'r': 1, 'd': 1 }

Notes

  1. Key Conversion: All keys are converted to strings
  2. Null Value Handling: Need to manually handle null and undefined values
  3. Performance: Time complexity is O(n)
  4. Case Sensitivity: Strings are case-sensitive

Differences from Other Functions

  • counting: Count element occurrences
  • countBy: Similar functionality, but may have different implementations
  • frequency: Similar functionality in other libraries

Performance

  • Time Complexity: O(n)
  • Space Complexity: O(k), where k is the number of unique elements
  • Use Cases: Data analysis and statistics

Released under the MIT License.