c语言++数组名【数字】

Problem statement: Write a C++ program to print all the non-repeated numbers in an array in minimum time complexity.

问题陈述:编写一个C ++程序, 以最小的时间复杂度所有未重复的数字打印在数组中

Input Example:

输入示例:

    Array length: 10
Array input: 2 5 3 2 4 5 3 6 7 3
Output:
Non-repeated numbers are: 7, 6, 4

Solution

Data structures used:

使用的数据结构:

    Unordered_map <int, int>

  • Key in the map is array value

    映射中的键是数组值

  • Value of key is frequency

    关键值是频率

Algorithm:

算法:

  1. Declare a map hash to store array elements as keys and to associate their frequencies with them.

    声明地图哈希,以将数组元素存储为键并将其频率与它们关联。

  2.     Unordered_map <int, int>hash;
    
    
  3. For each array element

    对于每个数组元素

    Insert it as key & increase frequencies. (0 ->1)

    将其作为键插入并增加频率。 (0-> 1)

    For same key it will only increase frequencies.

    对于相同的键,只会增加频率。

  4. For i=0: n-1
    hash[array [i]]++;
    End For
    
    
  5. Now to print the non-repeated character we need to print the keys (array elements) having value (frequency) exactly 1. (Non-repeating)

    现在要打印非重复字符,我们需要打印值(频率)正好为1的键(数组元素)。(非重复)

    Set an iterator to

    将迭代器设置为

    hash.begin().

    hash.begin() 。

    iterator->first is the key (array element) & iterator->second is the value( frequency of corresponding array value)

    iterator-> first是键(数组元素)& iterator-> second是值(对应数组值的频率)

  6. IF
    Iterator->second > 1
    Print iterator->first (the array element)
    END IF
    
    

Time complexity: O(n)

时间复杂度:O(n)

Explanation with example:

举例说明:

For this array: 2 5 3 2 4 5 3 6 7 3

对于此阵列: 2 5 3 2 4 5 3 6 7 3

The code:

代码:

for(int i=0;i<n;i++){//creating the map
hash[a[i]]++;//for same key increase frequency
}
.minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } } .minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } }

Actually does the following

实际上是以下

    At i=0
array[i]=2
Insert 2 & increase frequency
Hash:
Key(element)    Value(frequency)
2               1
At i=1
array[i]=5
Insert 5 & increase frequency
Hash:
Key(element)    Value(frequency)
2               1
5               1
At i=2
array[i]=3
Insert 3 & increase frequency
Hash:
Key(element)    Value(frequency)
2               1
5               1
3               1
At i=3
array[i]=2
Insert 2 increase frequency
'2' is already there, thus frequency increase.
Hash:
Key(element)    Value(frequency)
2               2
5               1
3               1
At i=4
array[i]=4
Insert 4 &increase frequency
Hash:
Key(element)    Value(frequency)
2               2
5               1
3               1
4               1
At i=5
array[i]=5
'5' is already there, thus frequency increase.
Hash:
Key(element)    Value(frequency)
2               2
5               2
3               1
4               1
At i=6
array[i]=3
'3' is already there, thus frequency increase.
Hash:
Key(element)    Value(frequency)
2               2
5               2
3               2
4               1
At i=7
array[i]=6
Insert 6, increase frequency.
Hash:
Key(element)    Value(frequency)
2               2
5               2
3               2
4               1
6               1
At i=8
array[i]=7
Insert 7, increase frequency.
Hash:
Key(element)    Value(frequency)
2               2
5               2
3               2
4               1
6               1
7               1
At i=9
array[i]=3
'3' is already there, thus frequency increase.
Hash:
Key(element)    Value(frequency)
2               2
5               2
3               3
4               1
6               1
7               1

Thus, Elements with frequency 1 are: 7, 6, 4

因此,频率为1的元素为:7、6、4

.minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } } .minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } }

C ++实现可在数组中按频率打印所有非重复数字 (C++ implementation to print all the Non-Repeated Numbers with Frequency in an Array)

#include <bits/stdc++.h>
using namespace std;
void findNonRepeat(int* a, int n){//Declare the map
unordered_map<int,int> hash;
for(int i=0;i<n;i++){//creating the map
hash[a[i]]++;//for same key increase frequency
}
cout<<"the nonrepeating numbers are: ";
//iterator->first == key(element value)
//iterator->second == value(frequency)
for(auto it=hash.begin();it!=hash.end();it++)
if(it->second==1)//frequency==1 means non-repeating element
printf("%d ",it->first);
printf("\n");
}
int main()
{int n;
cout<<"enter array length\n";
cin>>n;
int* a=(int*)(malloc(sizeof(int)*n));
cout<<"input array elements...\n";
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
//function to print repeating elements with their frequencies
findNonRepeat(a,n);
return 0;
}

Output

输出量

enter array length
10
input array elements...
2 5 3 2 4 5 3 6 7 3
the nonrepeating numbers are: 7 6 4

翻译自: https://www.includehelp.com/cpp-programs/print-all-the-non-repeated-numbers-in-an-array.aspx

c语言++数组名【数字】

c语言++数组名【数字】_C ++程序在数组中打印所有非重复数字相关推荐

  1. 整数数组查找java_使用Java编写程序以查找整数数组中的第一个非重复数字?

    查找数组中的第一个非重复数字-构造count数组以将给定数组中每个元素的计数存储为相同长度,且所有元素的初始值为0. 将数组中的每个元素与除自身之外的所有其他元素进行比较. 如果匹配发生,则增加其在计 ...

  2. c/c++ sizeof(数组名) 的解析 sizeof如何计算数组大小

    按照传统,先给结论:sizeof(数组名) 的值是这个数组所占据的内存的大小,单位是字节(bytes),在32位机器上,假设一个int型的数组a,里面一共有10个int类型元素,那么sizeof(a) ...

  3. 数组名的地址,和数组名取地址的讨论

    这个问题的发现是在以下的代码中发现的: #include<stdio.h> int main() {int arr[2]={1,2};printf("%p",arr); ...

  4. 对数组名取地址赋值给数组指针与普通指针的区别

    对数组名取地址赋值给数组指针与普通指针的区别: 以下红色字体的说法是错误的:     使用一级指针来接受&arr,只是表示一个普通的一级指针,     它并没有接收到&arr赋来的跨度 ...

  5. 1. 有1,2,3,4个数字, 能组成多少个互不相同且无重复数字的三位数? 都是多少?

    程序算法精题–JS版 有1,2,3,4个数字, 能组成多少个互不相同且无重复数字的三位数? 都是多少? 分析 百十个位数字都可以是1, 2, 3, 4 组成所有的排列后在去掉不满足条件的排列: 无重复 ...

  6. 用1、3、5、7 这4 个数字,能组成的互不相同且无重复数字的三位数有哪些?共有多少个?这些数的和为多少?

    #用1.3.5.7 这4 个数字,能组成的互不相同且无重复数字的三位数有哪些?共有多少个?这些数的和为多少?a={1,3,5,7} total=0 list = [] sum=0; for i in ...

  7. python(四个数字能生成多少个互不相同且无重复数字的三位数,实现命令行提示符)

    四个数字能生成多少个互不相同且无重复数字的三位数: """ 有1,2,3,4四个数字 求这四个数字能生成多少个互不相同且无重复数字的三位数 ""&qu ...

  8. 解析:数组名a、数组名取地址a、数组首地址a[0]、数组指针*p

    数组和指针向来就是傻傻分不清,当他们一起出现的时候就更加懵逼. 1 解析不同变量之间的区别: 数组名a: 数组名可以作为数组第一个元素的指针.我们由数组和指针的关系知道,a代表这个地址数值,它相当于一 ...

  9. java利用数组求平均值_Java程序使用数组计算平均值

    Java程序使用数组计算平均值 在此程序中,您将学习计算Java中给定数组的平均值. 示例:使用数组计算平均值的程序 public class Average { public static void ...

最新文章

  1. 阿里云 FTP 无法读取目录问题
  2. 技术人员关注的几个优质公众号
  3. Oracle note 基礎入門篇1
  4. Android移动开发之【Android实战项目】DAY3-滑动侧边栏SlidingMenu
  5. 大型网站核心架构要素--扩展性
  6. 记录java从左上到右下打印二维数组,从左下到右上打印二维数组
  7. Oracle数据库配置监听的作用
  8. VC中关于程序的托盘化
  9. linux查找有用日志常用技巧
  10. sklearn——model_selection——knn手写识别系统+iris分类
  11. 关于计算机审计建议,商业银行计算机审计存在问题与建议
  12. Linux 文件的打包与压缩
  13. 使用transferTo方法转换MultipartFile(处理NoSuchFileException异常)
  14. Python爬虫(1)
  15. Oracle EBS FSG报表运行无数据RG_RARG0004Error
  16. android显示地图代码,Android Studio之高德地图实现定位和3D地图显示(示例代码)
  17. python 整行_python dataframe 输出结果整行显示的方法
  18. 计算机科学界最高奖,中国科学家吴建平获国际互联网界最高奖
  19. LIS检验管理系统源码 医院管理系统源码
  20. AutoJs学习-文字转语音QQ发送

热门文章

  1. springframework报错_应对报错信息的必杀技!
  2. java jsp的指令_[javaEE] jsp的指令
  3. 微型计算机硬盘为什么要分区,为什么懂电脑的人,都说硬盘不需要分区?看完你就知道了...
  4. 山海伏妖录java_山海伏妖录攻略大全 剧情结局加点妖兽大全
  5. springboot超级详细的日志配置(基于logback)
  6. asp.net面试集合
  7. kodexplorer开源网盘php程序配置解析
  8. Kingback小组冲刺博客
  9. SVN命令行更新代码
  10. 服务器控件转换成HTML