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 ...
随机推荐
- SDK平台三态按钮的实现
Windows平台提供了丰富的控件,但是在使用中我们不会使用它提供的默认风格,有时候需要对控件进行改写,让它展现出更友好的一面,这次主要是说明三态按钮的实现. 三态按钮指的是按钮在鼠标移到按钮上时显示 ...
- 获取生日对应星座的PHP函数
PHP 获取指定日期对应的星座名称 /** * 获取指定日期对应星座 * * @param integer $month 月份 1-12 * @param integer $day 日期 1-31 * ...
- node读写json文件(进阶)
该方法可用于修改配置文件,直接上代码 fs.readFile('test1.json','utf8',function (err, data) { if(err) console.log(err); ...
- php:跨域
一个没那么难的历史难题,其实只要在被请求端,加一句: header('Access-Control-Allow-Origin: *'); 然后--然后没有了. //跨域访问的时候才会存在此字段 $or ...
- 4、File类之获取方法
这些方法也都是File类内置的成员方法,无需我们写,直接拿来用即可. 基本获取 public class Demo { public static void main(String[] args) { ...
- ajaxFileUpload 报这错jQuery.handleError is not a function
今天刚打个一个技术群,里面有人问标题上的问题,嘿,我恰好遇过,现在大家至少也在用jquery1.9以上的版本,ajaxfileupload的版本早就不更新了,大家可以下载看:地址这里, 它例子里使用的 ...
- js返回上一页并刷新代码整理
一:JS 重载页面,本地刷新,返回上一页 复制代码 代码如下: <a href="javascript:history.go(-1)">返回上一页</a> ...
- 百度JS模板引擎 baiduTemplate 1.0.6 版
A.baiduTemplate 简介 0.baiduTemplate希望创造一个用户觉得“简单好用”的JS模板引擎 注:等不及可以直接点左侧导航中的”C.使用举例“,demo即刻试用. 1.应用场景: ...
- 浙大pat1042题解
1042. Shuffling Machine (20) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Shu ...
- 4、Web应用程序中的安全向量 -- over-posting(重复提交)
模型绑定是ASP.NET MVC提供的强大功能,可遵照命名约定将输入元素映射到模型属性,从而极大地简化了处理用户输入的过程,然而,这也成为了攻击的另一种没接,给攻击者提供了一个填充模型属性的机会,右下 ...