# Algorithm in python to store and search daily occurrence for thousands of number

Storing and searching daily occurrences for thousands of numbers efficiently is a common problem in many applications. In this article, we will explore three different approaches to solve this problem using Python.

## Approach 1: Using a List

One simple way to store and search daily occurrences is by using a list. We can create a list of lists, where each inner list represents the daily occurrences for a specific number. The outer list will store these inner lists for all the numbers.

``````
# Initialize the list of lists
occurrences = [[] for _ in range(10000)]

# Store daily occurrences
def store_occurrence(number, day):
occurrences[number].append(day)

# Search for occurrences
def search_occurrences(number):
return occurrences[number]
``````

This approach allows us to store and search occurrences in constant time complexity O(1). However, it requires a large amount of memory to store the list of lists, especially if the range of numbers is large.

## Approach 2: Using a Dictionary

An alternative approach is to use a dictionary to store the daily occurrences. In this case, the keys of the dictionary will be the numbers, and the values will be lists of occurrences.

``````
# Initialize the dictionary
occurrences = {}

# Store daily occurrences
def store_occurrence(number, day):
if number in occurrences:
occurrences[number].append(day)
else:
occurrences[number] = [day]

# Search for occurrences
def search_occurrences(number):
return occurrences.get(number, [])
``````

This approach also allows us to store and search occurrences in constant time complexity O(1). It is more memory-efficient than the previous approach since it only stores occurrences for numbers that have occurred.

## Approach 3: Using a Database

If the number of daily occurrences is extremely large or if we need more advanced querying capabilities, using a database can be a better option. Python provides various libraries to interact with databases, such as SQLite, MySQL, or PostgreSQL.

Here is an example using SQLite:

``````
import sqlite3

# Connect to the database
conn = sqlite3.connect('occurrences.db')
cursor = conn.cursor()

# Create a table to store occurrences
cursor.execute('''
CREATE TABLE IF NOT EXISTS occurrences (
number INTEGER,
day INTEGER
)
''')

# Store daily occurrences
def store_occurrence(number, day):
cursor.execute('INSERT INTO occurrences VALUES (?, ?)', (number, day))
conn.commit()

# Search for occurrences
def search_occurrences(number):
cursor.execute('SELECT day FROM occurrences WHERE number = ?', (number,))
return [row[0] for row in cursor.fetchall()]

# Close the database connection
conn.close()
``````

This approach allows us to handle large amounts of data efficiently and provides more advanced querying capabilities. However, it requires additional setup and maintenance for the database.

After evaluating these three approaches, the best option depends on the specific requirements of the application. If memory usage is a concern and the range of numbers is not too large, Approach 2 using a dictionary is a good choice. If more advanced querying capabilities are needed or the number of occurrences is extremely large, Approach 3 using a database is recommended.

Rate this post

### 3 Responses

1. Raven Mcbride says:

Approach 2: Using a Dictionary seems so much simpler and efficient! Why complicate things?

2. Timothy Fuller says:

Approach 3 seems legit, but what if we try storing occurrences in a tree? 🌳

1. Isabel says:

Interesting idea! Storing occurrences in a tree could definitely be worth exploring. It might provide efficient searching and manipulation of data. However, we also need to consider the trade-offs and complexities involved in implementing and maintaining such a structure.