How to check duplicated value in array

package com.test;

import java.util.Set;
import java.util.HashSet;

public class CheckDuplicate
	public static void main(String args[])
		String [] sValue = new String[]{"a","b","c","d","","","e","a"};

			System.out.println("Check Normal : Value duplicated! \n");
			System.out.println("Check Set : Value duplicated! \n");


	//check duplicated value
	private static boolean checkDuplicated_withNormal(String[] sValueTemp)
		for (int i = 0; i < sValueTemp.length; i++) {
			String sValueToCheck = sValueTemp[i];
			if(sValueToCheck==null || sValueToCheck.equals(""))continue; //empty ignore
			for (int j = 0; j < sValueTemp.length; j++) {
					if(i==j)continue; //same line ignore
					String sValueToCompare = sValueTemp[j];
					if (sValueToCheck.equals(sValueToCompare)){
							return true;

		return false;


	//check duplicated value
	private static boolean checkDuplicated_withSet(String[] sValueTemp)
		Set<String> sValueSet = new HashSet<String>();
		for(String tempValueSet : sValueTemp)
			if (sValueSet.contains(tempValueSet))
				return true;
		return fals

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.