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 ...
随机推荐
- Python基础篇-day11 - 协程
本节主要内容: 1.Gevent协程2.Select\Poll\Epoll异步IO与事件驱动3.RabbitMQ队列 1.Gevent协程 1.1协程的好处 无需线程上下文切换的开销无需原子操作锁定及 ...
- 你会用::before、::after吗
::before和::after伪元素的用法 一.介绍 css3为了区分伪类和伪元素,伪元素采用双冒号写法. 常见伪类——:hover,:link,:active,:target,:not(),:fo ...
- 使用Toad创建存储过程出现错误并解决
存储过程中遇到ora-00942表或视图不存在 CREATE OR REPLACE PROCEDURE p IS CURSOR c IS SELECT * FROM scott.emp FOR UPD ...
- 【NOIP2007提高组】字符串展开
[题外话]这道题纯粹考验耐心,某些经常调程序调到摔键盘的人可以尝试 [题外话2]除了考耐心以外完全没有什么难点 [题外话3]也许会稍微恶心一点? [题外话4]其实我是在别人军训的时候滚来更博客的简直2 ...
- POJ 2313 Sequence#贪心
(- ̄▽ ̄)-* 找规律 //初始化为B[i]=A[i] //然后由V=|A[1]-B[1]|+|A[2]-B[2|+|A[3]-B[3]| // +|B[1]-B[2]|+|B[2]-B[3]| / ...
- HttpClient, HttpClientHandler, and WebRequestHandler介绍
注:本文为个人学习摘录,原文地址:https://blogs.msdn.microsoft.com/henrikn/2012/08/07/httpclient-httpclienthandler-an ...
- IO.Path路径
string filePath =@"E:/Randy0528/中文目录/JustTest.rar"; 更改路径字符串的扩展名.System.IO.Path.ChangeExten ...
- jquery-ui-multiselect 的使用
@model Gd.NetSign.Controllers.DTO.WsaleFundManageDTO @{ ViewBag.Title = "ShowDUYUN"; //Lay ...
- windows7旗舰版系统自带组件IIS搭建ftp
1.win7,”开始“,打开”控制面板“,点击”程序“,看到”程序和功能“,如图所示: 2.点击”打开或关闭Windows功能“,如图所示: 3.成功后,打开”控制面板“,点击”系统和安全“,点击”管 ...
- unity 退到桌面的 OnApplicationPause
void OnApplicationFocus( bool isFocus ) { // Debug.Log("--------OnApplicationPause---" + i ...