C语言实现deleteIdents()函数:移除字符数组连续重复行
Implementing
deleteIdents() to Remove Consecutive Duplicate Lines Alright, let's build that deleteIdents() function you need. The core idea is to strip consecutive duplicate lines from your character array, keeping only one instance of each run—and we only check the first 79 characters (MAXCHARS) of each line to decide if they're duplicates.
Key Approach
- Traverse your input line array, keeping track of the last line we added to our result set.
- For each line, compare its first 79 characters to the last line in the result. If they don't match, add the line to the result; if they do, skip it (since it's a consecutive duplicate).
- Handle edge cases like empty input gracefully.
Example Implementation (C Language)
Since you mentioned a character array, let's start with a C implementation—this is common for low-level character handling:
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXCHARS 79 // Returns the number of lines in the processed array, or -1 on memory failure int deleteIdents(char** lines, int line_count, char*** out_lines) { if (line_count == 0) { *out_lines = NULL; return 0; } // Allocate initial result array (worst case: no duplicates) *out_lines = malloc(sizeof(char*) * line_count); if (*out_lines == NULL) { return -1; } int result_count = 0; // Add the first line to our result (no previous line to compare) (*out_lines)[result_count++] = strdup(lines[0]); for (int i = 1; i < line_count; i++) { // Compare first MAXCHARS characters of current line vs last result line int is_consecutive_duplicate = (strncmp(lines[i], (*out_lines)[result_count - 1], MAXCHARS) == 0); if (!is_consecutive_duplicate) { (*out_lines)[result_count++] = strdup(lines[i]); } // Skip duplicates } // Shrink the result array to the exact number of lines we need char** optimized_result = realloc(*out_lines, sizeof(char*) * result_count); if (optimized_result != NULL) { *out_lines = optimized_result; } return result_count; }
How It Works
strncmpdoes the heavy lifting: it compares exactly the first 79 characters of two strings, which matches your requirement perfectly.strdupcopies each unique line to the result array, so we don't modify the original input lines.- We start with a full-size array then shrink it with
reallocto save memory.
Example Implementation (Python)
If you're working in a higher-level language like Python, the logic is identical but simpler to write:
MAXCHARS = 79 def deleteIdents(lines): if not lines: return [] processed_lines = [lines[0]] # Store the first 79 chars of the last added line for quick comparison last_checked = lines[0][:MAXCHARS] for line in lines[1:]: current_checked = line[:MAXCHARS] if current_checked != last_checked: processed_lines.append(line) last_checked = current_checked return processed_lines
Test It With Your Example
original_lines = [ "Hello World", "Hi World", "Hello World", "Hello World", "Hello World", "Hi there" ] print(deleteIdents(original_lines)) # Output: ['Hello World', 'Hi World', 'Hello World', 'Hi there']
Notes
- For C: Don't forget to free the memory returned by
deleteIdents—loop through each line andfreeit, thenfreethe array itself. - If your input is a 2D char array (e.g.,
char lines[100][MAXCHARS+1]), adjust the C function to copy characters directly instead of usingstrdup. - This only removes consecutive duplicates—non-consecutive duplicates (like the first and third lines in your example) are kept, which matches your requirements.
内容的提问来源于stack exchange,提问作者superluigisunshine
相关产品推荐
相关产品推荐

