Find how many times a value appears in a 2D array and store that number in another 1D array

2 min read 05-10-2024
Find how many times a value appears in a 2D array and store that number in another 1D array


Counting Occurrences in a 2D Array: A Comprehensive Guide

Problem: You have a 2D array filled with values, and you need to find out how many times each unique value appears in the array. The results should be stored in a separate 1D array, where each element corresponds to the count of its respective value.

Rephrased: Imagine you have a grid of numbers. You want to know how many times each different number appears in the entire grid. You want to create a list where each number is paired with its count in the grid.

Let's dive into an example:

Consider the following 2D array:

array = [
    [1, 2, 3],
    [2, 3, 1],
    [1, 1, 2]
]

We need to find how many times each unique value (1, 2, and 3) appears in this array and store the results in a 1D array.

Original Code (Python):

array = [
    [1, 2, 3],
    [2, 3, 1],
    [1, 1, 2]
]

unique_values = set()
for row in array:
    for value in row:
        unique_values.add(value)

counts = [0] * len(unique_values)
for row in array:
    for value in row:
        index = list(unique_values).index(value)
        counts[index] += 1

print(counts)

Explanation and Analysis:

The code first creates a set unique_values to store all the unique values present in the array. This removes duplicates automatically. Then, it initializes a 1D array counts with the same length as unique_values, filled with zeros. The code iterates through the 2D array, and for each value encountered, it finds its index in unique_values and increments the corresponding count in counts.

Optimization:

The above code works, but we can improve its efficiency. Using a dictionary to store counts is a more efficient solution, especially for larger arrays.

Optimized Code (Python):

array = [
    [1, 2, 3],
    [2, 3, 1],
    [1, 1, 2]
]

counts = {}
for row in array:
    for value in row:
        if value in counts:
            counts[value] += 1
        else:
            counts[value] = 1

print(counts)

Explanation:

This optimized version iterates through the array and uses a dictionary counts to store the counts for each value. If a value is already present in counts, its count is incremented; otherwise, it's added to the dictionary with a count of 1.

Advantages of using a dictionary:

  • Efficiency: Dictionaries provide fast lookups (O(1) time complexity), making the code more efficient for large arrays.
  • Simplicity: The code becomes more concise and easier to understand.
  • Flexibility: Dictionaries allow for flexible handling of different value types, including strings, lists, and even custom objects.

Conclusion:

Counting the occurrences of values in a 2D array can be effectively accomplished using dictionaries, offering a more efficient and readable solution compared to traditional array-based methods. This approach is particularly beneficial for large arrays where performance is critical.

Further Resources:

By understanding the principles and applying appropriate data structures, you can efficiently count occurrences in 2D arrays and gain valuable insights from your data.