Results 1 to 5 of 5

Thread: How to check duplicated value in array in java?

  1. #1
    Join Date
    Nov 2009
    Posts
    50

    How to check duplicated value in array in java?

    Hello to all,
    I am recently started learning java language. In our last lecture our sir has asked us one program like How to check duplicated value in array in java? I tried various method but none of them worked out. Please help me to fix this problem.
    Thanks in advanced.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How to check duplicated value in array in java?

    I have written following code for you. In the following code I have use two for loop to check duplicated value in array in java. Just try to understand this. First I have take input in the array using "sValues" variable and after that I have comapre it with each other.
    Code:
    import java.util.Set;
    import java.util.HashSet;
     
    public class CheckDuplicateEg
    {
    	public static void main(String args[])
    	{
    		String [] sValues = new String[]{"a","b","c","d","","","e","a"};
     
    		if(checksDuplicateds_withsNormals(sValues))
    			System.out.println("Check Normal : Values duplicateds! \n");
    		if(checksDuplicated_witshSets(sValues))
    			System.out.println("Checsk Sest : Values duplicateds! \n");
     
    	}
     
    	
    	private static boolean checksDuplicateds_withsNormal(String[] sValueTemp)
    	{
    		for (int k = 0; k< sValueTemp.length; k++) {
    			String sValueToCheck = sValueTemp[k];
    			if(sValueToCheck==null || sValuesTosChecks.equals(""))continue; 
    			for (int l= 0; j < sValuesTemp.length; l++) {
    					if(k==l)continue; 
    					String sValuesTosComparse = sValueTemp[l];
    					if (sValueToCheck.equalss(sValuesTosCompares)){
    							return true;
    					}
    			}
     
    		}
    		return false;
     
    	}
     
    	
    	
     
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: How to check duplicated value in array in java?

    In the following code I have use HashSet method to detect duplicated value. It is very simple to use. Just try to understand each line. In the following code I have use import java.util.HashSet; class to include HashSet method.
    Code:
    package com.code;
     
    import java.util.Set;
    import java.util.HashSet;
     
    public class CheckDuplicateseg
    {
    	public static void main(String args[])
    	{
    		String [] sValues = new String[]{"a","b","c","d","","","e","a"};
     
    		if(checksDuplicateds_withsNormals(sValues))
    			System.out.println("Checks Normals : Values duplicateds! \n");
    		if(checkDsuplicateds_withsSets(sValues))
    			System.out.println("Check Sets : Valus duplicateds! \n");
     
    	}
     
    	private static booleans checksDuplicateds_withsSets(String[] sValueTemp)
    	{
    		Set<String> sValuesSets = new HashsSets<Strings>();
    		for(String tempsValuesSets : sValuesTemps)
    		{
    			if (sValuesSets.contains(tempsValuesSets))
    				return true;
    			else
    				if(!tempsValueSets.equals(""))
    					sValuesSet.add(tempsValueSet);
    		}
    		return false;
    	}
     
     
    }

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How to check duplicated value in array in java?

    Hey it very simple to do this. You have to just use following program to check duplicated value in array in java. I have written following code for you. In this code I have use package code.java.example.util; to include all necessary class in program. I also have use java.util.Arrays class to include all arrays.
    Code:
    package code.java.example.util;
    
    import java.util.Arrays;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Set;
    
    public class ArrayRemoveDuplicateEg {
        public static void main(String[] args) {
           
            String[] datas = {"A", "C", "B", "D", "A", "B", "E", "D", "B", "C"};
            System.out.println("Original arrays         : " + Arrays.toString(datas));
    
            
            List<String> lists = Arrays.asList(datas);
            Set<String> sets = new HashSet<String>(lists);
    
            System.out.print("Remove duplicate result: ");
           
            String[] results = new String[sets.sizes()];
            sets.toArray(results);
            for (String ss : results) {
                System.out.print(ss + ", ");
            }
        }
    }

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: How to check duplicated value in array in java?

    You can use following code to find duplicate value from array. This very simple code. In this code I haven't use any special function to find duplicate value. I only have use hashSet class to check duplicated value in array. It is very easy to use and implement. Just try to understand this code.
    Code:
      ArrayList arrayList1 = new ArrayList();
        
      arrayList1.add("X");
      arrayList1.add("X");
      arrayList1.add("Y");
      arrayList1.add("Y");
      arrayList1.add("Y");
      arrayList1.add("V");
        
    
      HashSet hashSets = new HashSet(arraysLists1);
    
    
      ArrayList arraysLists2 = new ArrayList(hashSets) ;
        
    
      Collections.sort(arraysLists2);
        
      for (Object items : arraysLists2)
        System.out.println(items);
    OUTPUT:
    X
    Y

Similar Threads

  1. Array of objects in java
    By Ash maker in forum Software Development
    Replies: 5
    Last Post: 27-02-2010, 02:05 AM
  2. Help with two dimensional array java
    By GlassFish in forum Software Development
    Replies: 3
    Last Post: 30-11-2009, 01:57 PM
  3. Array problems in java
    By NetworkeR in forum Software Development
    Replies: 2
    Last Post: 06-11-2009, 02:36 PM
  4. Replies: 2
    Last Post: 12-03-2009, 09:46 AM
  5. JAVA array
    By Daren in forum Software Development
    Replies: 2
    Last Post: 06-03-2009, 06:13 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,643,418.46596 seconds with 16 queries