Java实现列表/数组重复数字查找 | 圆周率计算应用优化需求
Hey there! Building your own Pi Finder app from scratch is such a cool project—even with existing tools out there, the learning curve and sense of accomplishment are totally worth it. Let's fix that slow duplicate detection issue you're facing with your sequence data.
First, let's clarify two common scenarios you might be dealing with, since your example mentions both individual repeated numbers (4,7,6) and a repeating subsequence (4,7,6 looping):
1. Finding Individual Repeated Numbers
If you just need to identify which numbers show up more than once in your sequence, a dictionary-based frequency count is your best bet—it's O(n) time complexity (way faster than nested loops) and uses minimal extra space.
Here's a Python implementation tailored for your use case:
def find_repeated_numbers(sequence): number_counts = {} for num in sequence: # Increment count for the number, default to 0 if not seen before number_counts[num] = number_counts.get(num, 0) + 1 # Filter and return numbers that appear more than once return [num for num, count in number_counts.items() if count > 1] # Example usage with your sequence sample_sequence = [1,2,5,4,7,6,4,7,6,4,7] print(find_repeated_numbers(sample_sequence)) # Output: [4,7,6]
This will quickly flag all duplicate individual numbers without bogging down your app, even as your sequence grows longer.
2. Detecting Repeating Subsequences (Like the 4,7,6 Loop)
If you're trying to spot repeating patterns (like the cycle of 4,7,6 in your example), you'll need a method to check for recurring contiguous sub-sequences. Here's a practical approach using a dictionary to track sub-sequences:
def find_repeating_subsequence(sequence, min_pattern_length=3): sequence_length = len(sequence) # Check patterns starting from the minimum desired length up to half the sequence length for pattern_length in range(min_pattern_length, sequence_length // 2 + 1): subseq_tracker = {} for start_idx in range(sequence_length - pattern_length + 1): # Convert sub-sequence to a tuple (since lists can't be dictionary keys) current_subseq = tuple(sequence[start_idx:start_idx+pattern_length]) if current_subseq in subseq_tracker: # Found a repeated pattern! Return it immediately return list(current_subseq) else: subseq_tracker[current_subseq] = start_idx # No repeating pattern found return None # Example usage print(find_repeating_subsequence(sample_sequence)) # Output: [4,7,6]
This will catch the repeating 4,7,6 pattern in your sample sequence. You can adjust min_pattern_length if you're looking for shorter or longer cycles.
Bonus: Optimizations for Your Pi Finder
Since you're using the Leibniz formula (which converges very slowly—you'll need tons of iterations to get meaningful π digits), here are two key optimizations:
- Use generators instead of lists: Instead of storing every digit in a list upfront, generate digits one at a time and run duplicate checks incrementally. This saves memory and lets you stop early if you find the pattern you're looking for.
- Avoid nested loops: If your original slowdown came from checking duplicates with nested loops (like comparing every element to every other element), swapping to the dictionary-based methods above will drastically speed things up.
Hope this helps you get your Pi Finder running smoothly—happy coding!
内容的提问来源于stack exchange,提问作者WLA

