[算法]对于已排好序的数组进行去重

最近项目中遇到一个场景,需要对排好序的数组进行去重(场景就是对同时获取两个频道的消息列表时做一个合并,而消息列表里的数据已经是排序好的),

经过思考后,尝试写了一个,感觉还算简洁,这里分享一下,如有有缘人可以参考,遇到问题还望指正,欢迎讨论:)

 1 #include <iostream>
 2
 3 using namespace std;
 4
 5 #define NUM  7
 6
 7 int main(){
 8
 9         int a[NUM] = {1, 1, 2, 2, 3, 5, 5};
10
11         int *p1 = a, *p2 = a;
12         while (p1 != a + NUM){
13                 if (*p1 == *p2){
14                         p1++;
15                         continue;
16
17                 }else{
18                         p2++;
19                         if (p2 < p1){
20                                 *p2 = *p1;
21                         }
22                         continue;
23                 }
24
25
26         }
27
28         int *p = a;
29         while(p <= p2){
30                 cout << *p++ << ‘ ‘;
31
32         }
33
34         cout << endl;
35
36
37 }
时间: 2024-07-31 12:23:03

[算法]对于已排好序的数组进行去重的相关文章

【算法导论学习-016】两个已排过序的等长数组的中位数(median of two sorted arrays)

问题来源 <算法导论>P223 9.3-8: Let X[1..n] and Y[1..n] be two arrays, each containing nnumbers already in sorted order. Give an O(lgn)-time algorithm to find themedian of all 2n elements in arrays X and Y. 翻译过来即:求两个等长(n个元素)的已排序数组A和B的中位数 方案1:对两个数组进行归并直到统计到第n

[经典面试题]输入一个排好序的数组的一个旋转,输出旋转数组的最小元素。

[题目] 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个排好序的数组的一个旋转,输出旋转数组的最小元素.例如数组{3, 4, 5, 1, 2}为{1, 2, 3, 4, 5}的一个旋转,该数组的最小值为1. [分析] 这道题最直观的解法并不难.从头到尾遍历数组一次,就能找出最小的元素,时间复杂度显然是O(N).但这个思路没有利用输入数组的特性,我们应该能找到更好的解法. 我们注意到旋转之后的数组实际上可以划分为两个排序的子数组,而且前面的子数组的元素都大于或者等于后面

58.有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中

#include<iostream> using namespace std; int main() { int n,k; int temp; int a[11]={1,4,6,9,11,13,35,44,46,50}; cout<<"原数组是:"<<endl; for(int p=0;p<10;p++) { cout<<a[p]<<" "; } cout<<endl; cout<&

将两个排好序的数组,合并到另外一个数组中,并且合并之后的数组也是有序的。

int a[3] = {12, 15, 17}; int b[4] = { 2, 8, 16, 22}; int c[7] = {0}; int i = 0, j = 0, k = 0; while (i < 3 && j < 4 ) { if (a[i] > b[j]) { c[k++] = b[j++]; } else { c[k++] = a[i++]; } } while (i < 3) { c[k++] = a[i++]; } while (j <

有一个从小到大排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。

<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title></title> <script type="text/javascript"> //有一个从小到大排好序的数组.现输入一个数,要求按原来的规律将它插入数组中. var arr = [1, 2, 3, 4, 5, 7, 9]; function insertScot

如何快速找到排好序的数组中最先不连续的数字N

现在有一大堆自然数组成的小到大数组arr,其中会有123456910  这样就要找到6(最先不连续的数字) 举例:[12356789] 找到3 [012345678] 找到8 第一种:遍历数组判断是否差1,返回值即可   算法时间复杂度O(n) 第二种: /* * 传输方向:指挥机?用户机 *指令长度:共3 bytes *说明:本指令为累积式应答,指令中的序号表示指挥机已经接收到了一直到该序号的所有定位记录,例如: *假设指挥机已接收到定位记录序号为0,1,2,3,5,6,8,则收到应答指令中的

一个排好序的数组,找出两数之和为m的所有组合

public static void getGroup(int sum, int arr[]){ if(arr.length<=0) { System.out.println("数组为空"); return; } int start = 0; int end =arr.length-1; while (start <end){ if(arr[start]+arr[end]== sum){ System.out.println("符合要求"+arr[sta

有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。

public static void main(String[] args) { int[] a = {1,3,5,7,9,19,29,70}; int[] b = new int[a.length+1]; Scanner scan = new Scanner(System.in); System.out.print("请输入一个数:"); int insert = scan.nextInt(); int n = 0; //当插入的数小于第一个数时 if(insert < a[0

一个排好序的数组,找出两数之和为x的所有组合【双指针】

#include <bits/stdc++.h> using namespace std; const int N = 1e6,INF = 0x3f3f3f3f; int a[N]; int n,x; void fun(int n) { int left = 0, right = n-1; while(left < right) { int ans=a[left] + a[right]; if(ans == x) { cout<<a[left]<<' '<&