# counter

Published articles for counter.

This is one page of public article previews, not the complete archive. Follow Next page to continue. Summaries are not the original full articles.

## Intuitive Proofs for Linked-List Loop Detection and Majority-Element Algorithms

DevFeed: [Intuitive Proofs for Linked-List Loop Detection and Majority-Element Algorithms](<https://devfeed.tech/articles/relatively-non-obvious-tricks-in-solving-simple-algorithmic-problems-38655.md>)

Original publisher: [Read original article](<https://krossovochkin.com/posts/2024_12_21_relatively_non_obvious_tricks_in_solving_simple_algorithmic_problems/>)

Published: 2024-12-21T00:00:00Z

Content type: tutorial

Language: en

Sources: [Vasya Drobushkov](<https://devfeed.tech/sources/vasya-drobushkov.md>)

Topics: [Algorithms, Complexity](<https://devfeed.tech/topics/algorithms-complexity.md>), [Algorithms](<https://devfeed.tech/topics/algorithms.md>), [LeetCode](<https://devfeed.tech/topics/leetcode.md>), [Programming](<https://devfeed.tech/topics/programming.md>)

Tags: [algorithms](<https://devfeed.tech/tags/algorithms.md>), [array](<https://devfeed.tech/tags/array.md>), [code](<https://devfeed.tech/tags/code.md>), [collections](<https://devfeed.tech/tags/collections.md>), [counter](<https://devfeed.tech/tags/counter.md>), [examples](<https://devfeed.tech/tags/examples.md>), [memory](<https://devfeed.tech/tags/memory.md>), [pointers](<https://devfeed.tech/tags/pointers.md>), [solutions](<https://devfeed.tech/tags/solutions.md>), [time](<https://devfeed.tech/tags/time.md>)

### AI overview

This tutorial explains why concise solutions to seemingly easy algorithmic problems work. It uses linked-list loop detection with slow and fast pointers and majority-element detection with a counter, emphasizing intuitive proofs over implementation alone.

### Source excerpt

Blowing the dust off LeetCode once again, I found myself, as in the past, struggling with coding relatively simple algorithms. Just like with anything else, if you don't practice for years, you lose some of the hands-on experience. On the positive side, I noticed that for some problems, my new submissions were much better and more concise compared to my old ones. That's an awesome feeling--a tangible measure of growth. While tackling certain easy problems, I realized that "easy" usually just means "doesn't require much code." However, the idea behind the optimal solution might still not be very intuitive. Sure, one can use brute force or additional collections, but in most cases, this leads to either a "time limit exceeded" or an "out of memory" error.

## Finding the majority element of a stream

DevFeed: [Finding the majority element of a stream](<https://devfeed.tech/articles/finding-the-majority-element-of-a-stream-40379.md>)

Original publisher: [Read original article](<https://www.jeremykun.com/2015/03/09/finding-the-majority-element-of-a-stream/>)

Published: 2015-03-09T09:00:11Z

Content type: tutorial

Language: en

Sources: [Jeremy Kun](<https://devfeed.tech/sources/jeremy-kun.md>)

Topics: [Algorithm](<https://devfeed.tech/topics/algorithm.md>), [Python](<https://devfeed.tech/topics/python.md>), [data](<https://devfeed.tech/topics/data.md>)

Tags: [algorithm](<https://devfeed.tech/tags/algorithm.md>), [counter](<https://devfeed.tech/tags/counter.md>), [data-mining](<https://devfeed.tech/tags/data-mining.md>), [frequency-moments](<https://devfeed.tech/tags/frequency-moments.md>), [mathematics](<https://devfeed.tech/tags/mathematics.md>), [pair](<https://devfeed.tech/tags/pair.md>), [programming](<https://devfeed.tech/tags/programming.md>), [python](<https://devfeed.tech/tags/python.md>), [space](<https://devfeed.tech/tags/space.md>), [stream](<https://devfeed.tech/tags/stream.md>), [streaming-algorithms](<https://devfeed.tech/tags/streaming-algorithms.md>), [streaming-data](<https://devfeed.tech/tags/streaming-data.md>), [sublinear-space](<https://devfeed.tech/tags/sublinear-space.md>)

### AI overview

This article presents a Python algorithm for finding the value that occurs more than half the time in a massive data stream. It explains the pairing-based correctness argument, single-pass operation, O(log(n) + log(m)) space usage, the necessity of the majority guarantee, and a k-counter generalization for detecting frequent items.

### Source excerpt

Problem: Given a massive data stream of $ n$ values in $ \{ 1, 2, \dots, m \}$ and the guarantee that one value occurs more than $ n/2$ times in the stream, determine exactly which value does so. Solution: (in Python) def majority(stream): held = next(stream) counter = 1 for item in stream: if item == held: counter += 1 elif counter == 0: held = item counter = 1 else: counter -= 1 return held Discussion: Let's prove correctness.