find unique values in an array
Problem:
given an array that contains duplicates (except one value), find the one value that does not have a duplicate in that array. Explain the complexity of your algorithm. So in the array: A = [2, 3, 4, 5, 2, 3, 4] your algorithm should return 5 since that's the value that does not have a duplicate.
Solution:
this can be solved using a HashMap (two pass through). Or 2 HashSets (1 pass through).
I will show the 2nd solution (2 hashset)
import java.util.*; public class Dup{ public static void main(String[] args){
int[] n= {2,3,4,5,2,3,4,1,1,1};
Set<Integer> all= new HashSet<>();
Set<Integer> unq= new HashSet<>(); for(int i:n){
if(all.contains(i)){
unq.remove(i);
}
else{
all.add(i);
unq.add(i);
}
} Iterator i= unq.iterator();
while(i.hasNext()){
System.out.println(""+ i.next());
} } }
find unique values in an array的更多相关文章
- Fast Algorithm To Find Unique Items in JavaScript Array
When I had the requirement to remove duplicate items from a very large array, I found out that the c ...
- SharePoint 2013 Content Deployment 报错 These columns don't currently have unique values
错误描述: These columns don't currently have unique values. Content deployment job 'job name' failed.The ...
- Choose unique values for the 'webAppRootKey' context-param in your web.xml files!
在Tomcat的server.xml中配置两个context,出现其中一个不能正常启动,交换配置顺序,另一个又不能正常启动,即始终只有第二个配置能启动的情况.如果单独部署,都没有问题.报错大致内容如下 ...
- [Ramda] Get a List of Unique Values From Nested Arrays with Ramda (flatMap --> Chain)
In this lesson, we'll grab arrays of values from other arrays, resulting in a nested array. From the ...
- Find Unique pair in an array with pairs of numbers 在具有数字对的数组中查找唯一对
给定一个数组,其中每个元素出现两次,除了一对(两个元素).找到这个唯一对的元素. 输入:第一行输入包含一个表示测试用例数的整数T.然后T测试用例如下.每个测试用例由两行组成.每个测试用例的第一行包含整 ...
- tomcat下部署了多个项目启动报错java web error:Choose unique values for the 'webAppRootKey' context-param in your web.xml files
应该是tomcat下部署了多个项目且都使用log4j. <!--如果不定义webAppRootKey参数,那么webAppRootKey就是缺省的"webapp.root". ...
- Choose unique values for the 'webAppRootKey' context-param in your web.xml files! 错误的解决
大意是Log4jConfigListener在获取webapp.root值时,被后一context的值替换掉了,所以要在各个项目的web.xml中配置不同的webAppRootKey值,随即在其中一个 ...
- 【LeetCode】1471. 数组中的 k 个最强值 The k Strongest Values in an Array (Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 自定义排序 日期 题目地址:https://leetc ...
- [转]JavaScript去重的6种方法
Array.prototype.unique1 = function() { var n = []; for(var i = 0; i < this.length; i++) { if (n.i ...
随机推荐
- JVM内存模型,垃圾回收算法
JVM内存模型总体架构图 程序计数器多线程时,当线程数超过CPU数量或CPU内核数量,线程之间就要根据时间片轮询抢夺CPU时间资源.因此每个线程有要有一个独立的程序计数器,记录下一条要运行的指令.线程 ...
- 在hadoop 的任务中设置 map数量
试验了一下: 调整mapred-site.xml中mapred.min.split.size的值可以改变map的数量 首先设置了hdfs-site.xml中的dfs.block.size为20M,测试 ...
- linux远程登录ssh免密码
原文链接,感谢原作者. (一)问题: 假如我们现在有两台机器:ServerA和ServerB,现在想要让ServerA不用输入密码就能够进行访问. (二)方法和原理: 我们使用ssh-keygen在S ...
- gulp4个基础API
Gulp.src(globs[, options]) 此接口会匹配工作目录下指定规则的文件并返回提供给下一个插件管道使用.其中globs就是匹配格式,options是一些额外参数. gulp.src( ...
- Retrofit,Rxjava,OkHttp3的配置
这几个库的版本都更新了,和以前的使用略有不同,这是两篇介绍的博客:http://www.jianshu.com/p/91ac13ed076d ,https://drakeet.me/retrofit- ...
- CascadeType
当Hibernate配置了(JPA注解) cascade = { CascadeType.PERSIST, CascadeType.MERGE } 调用保存时 session.save(user); ...
- Jquery获得 selection的text 和 option值
Jquery获得 selection的text 和 option值 <select id="accountStatus" editable="false" ...
- hdu_5904_LCIS(DP)
题目链接:hdu_5904_LCIS 题意: 给你两串数,让你找这两串数的最长公共子序列,并且这个最长公共子序列是连续的数值 题解: 我们首先先分别处理出a,b的每个数的最长连续的长度 然后随便找一串 ...
- hdu_5884_Sort(二分+单调队列)
题目链接:hdu_5884_Sort 题意: 有n个数,每个数有个值,现在你可以选择每次K个数合并,合并的消耗为这K个数的权值和,问在合并为只有1个数的时候,总消耗不超过T的情况下,最小的K是多少 题 ...
- lldpd启动脚本分析
#!/bin/sh /etc/rc.common # Copyright (C) 2008-2012 OpenWrt.org #启动顺序 START=90 #创建PID文件 SERVICE_USE_P ...