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());
        }

    }

}
时间: 2024-10-14 10:33:54

find unique values in an array的相关文章

SharePoint 2013 Content Deployment 报错 These columns don&#39;t currently have unique values

错误描述: These columns don't currently have unique values. Content deployment job 'job name' failed.The exception thrown was 'System.ArgumentException' : 'These columns don't currently have unique values.' 错误截图,如下图: 错误日志位置,如下图: 在服务器上找到错误日志的位置,是压缩包,记得找对G

Choose unique values for the &#39;webAppRootKey&#39; context-param in your web.xml files!

在Tomcat的server.xml中配置两个context,出现其中一个不能正常启动,交换配置顺序,另一个又不能正常启动,即始终只有第二个配置能启动的情况.如果单独部署,都没有问题.报错大致内容如下: appears to have started a thread named [com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0] but has failed to stop it. This is very lik

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

应该是tomcat下部署了多个项目且都使用log4j. <!--如果不定义webAppRootKey参数,那么webAppRootKey就是缺省的"webapp.root".但最好设置,以免项目之间的名称冲突. 定义以后,在Web Container启动时将把ROOT的绝对路径写到系统变量里. 然后log4j的配置文件里就可以用${webName.root }来表示Web目录的绝对路径,把log文件存放于webapp中. 此参数用于后面的“Log4jConfigListener”

java web error:Choose unique values for the &#39;webAppRootKey&#39;

使用tomcat部署了两个app,发现始终有一个默认启动不起来.从tomcat日志中有了发现. 日志信息: 四月 19, 2016 9:09:55 上午 org.apache.catalina.core.StandardContext listenerStartSEVERE: Exception sending context initialized event to listener instance of class ch.qos.logback.ext.spring.web.Logback

Choose unique values for the &#39;webAppRootKey&#39; context-param in your web.xml files! 错误的解决

大意是Log4jConfigListener在获取webapp.root值时,被后一context的值替换掉了,所以要在各个项目的web.xml中配置不同的webAppRootKey值,随即在其中一个web.xml中添加: <context-param> <param-name>webAppRootKey</param-name> <param-value>web.sample.root</param-value> </context-pa

[转]JavaScript去重的6种方法

Array.prototype.unique1 = function() { var n = []; for(var i = 0; i < this.length; i++) { if (n.indexOf(this[i]) == -1) n.push(this[i]); } return n; } Array.prototype.unique2 = function() { var n = {},r=[]; for(var i = 0; i < this.length; i++) { if

js数组去重6法解析

1,建个新数组,遍历老数组,若在新数组里没找到,则将这个元素放到新数组,然后返回 Array.prototype.unique1 = function() { var n = []; for(var i = 0; i < this.length; i++) { if (n.indexOf(this[i]) == -1) n.push(this[i]); } return n; } 2,利用hash表,将放到新数组的元素在hash表中存true值,下次遍历数组时检查元素在hash表中的值 Arra

945. Minimum Increment to Make Array Unique

945. Minimum Increment to Make Array Unique Medium 25111FavoriteShare Given an array of integers A, a move consists of choosing any A[i], and incrementing it by 1. Return the least number of moves to make every value in A unique. Example 1: Input: [1

lodash学习笔记之Array方法

今天周末在家无聊学习一下lodash. lodash目前的中文资料很少.而且api好像还被墙了.下面说一下lodash的arrary相关的方法. 1. chunk   英 [t???k]    顾名思义,是对数组进行分块的方法 n. 大块:矮胖的人或物 用法: _.chunk(array,number)  根据number对array进行均等的分块,如果array不能被number平分,则会留下一个余下的块. _.chunk(['a','b','c','d'],-1); //当 size<=1的