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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. Choose unique values for the 'webAppRootKey' context-param in your web.xml files!

    在Tomcat的server.xml中配置两个context,出现其中一个不能正常启动,交换配置顺序,另一个又不能正常启动,即始终只有第二个配置能启动的情况.如果单独部署,都没有问题.报错大致内容如下 ...

  4. [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 ...

  5. Find Unique pair in an array with pairs of numbers 在具有数字对的数组中查找唯一对

    给定一个数组,其中每个元素出现两次,除了一对(两个元素).找到这个唯一对的元素. 输入:第一行输入包含一个表示测试用例数的整数T.然后T测试用例如下.每个测试用例由两行组成.每个测试用例的第一行包含整 ...

  6. tomcat下部署了多个项目启动报错java web error:Choose unique values for the 'webAppRootKey' context-param in your web.xml files

    应该是tomcat下部署了多个项目且都使用log4j. <!--如果不定义webAppRootKey参数,那么webAppRootKey就是缺省的"webapp.root". ...

  7. Choose unique values for the 'webAppRootKey' context-param in your web.xml files! 错误的解决

    大意是Log4jConfigListener在获取webapp.root值时,被后一context的值替换掉了,所以要在各个项目的web.xml中配置不同的webAppRootKey值,随即在其中一个 ...

  8. 【LeetCode】1471. 数组中的 k 个最强值 The k Strongest Values in an Array (Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 自定义排序 日期 题目地址:https://leetc ...

  9. [转]JavaScript去重的6种方法

    Array.prototype.unique1 = function() { var n = []; for(var i = 0; i < this.length; i++) { if (n.i ...

随机推荐

  1. linux下安装LoadRunner LoadGenerator

    root用户登录 关闭防火墙: setenforce 0 /etc/init.d/iptables stop 先安装一个rpm包,compat-libstdc++-33-3.2.3-61.i386.r ...

  2. java 枚举类型和数据二进制等问题思考

    .以下代码的输出结果是什么? int X=100; int Y=200; System.out.println("X+Y="+X+Y); System.out.println(X+ ...

  3. LEK-Introduction

    LEK - logstash + elasticsearch + Kibana Elasticsearch, Logstash, and Kibana — designed to take data ...

  4. Hibernate5-课程笔记2

    单表的增删改查操作: (1)定义获取Session和SessionFactory的工具类: package com.tongji.utils; import org.hibernate.Session ...

  5. php中的curl常用例子

    1.基本请求 <?php $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://www.baidu.com"); ...

  6. npm 使用代理

    npm install 有时候会安装失败,可能是网络的问题,可以使用代理来安装 npm获取配置有6种方式,优先级由高到底. 命令行参数. --proxy http://server:port即将pro ...

  7. Javaweb 第5天 mysql 数据库课程

    MySQL数据库课程 两日大纲 ● 数据库的概念.MySQL快速入门.SQL语言简介 ● 数据库操作.表操作.数据记录操作.数据类型和约束 ● 查询 ● 多表关系.多表连接查询 ● 视图 ● 数据备份 ...

  8. java学习笔记之-构造函数

    目的: 数据的初始化是非常重要的,未经初始化的数据很多情况下都不是期望的结果,很多忘记初始化的数据是导致错误的根源.构造函数就是提供了一个初始化的地方,可以把初始化数据的代码写在构造函数里.当然允许你 ...

  9. Git回退到服务器某个版本正确姿势

    背景: Git协作中,成员不可避免地会提交一些错误的版本,由于Git相比SVN引入了本地仓库,操作会相对复杂,以下为姿势分解 找一个源文件RspUtils.java,加上一行注释 //测试回退git服 ...

  10. Sass与Compress实战:第五章

    概要:第5章展示了Compass如何使你免去编写跨浏览器的CSS3的痛苦. 本章内容: ● 用Compass的CSS3模块创建跨浏览器的CSS3样式表 ● 在低版本IE中支持一些CSS3的特性 ● C ...