2017  Kodetalk | Feedback | Privacy Policy | Terms | About
userimage

best way to check array having duplicate value or not.

i want to know the best way to check that the string have duplicate value or not.

 Let's say i have this value and i want to check the duplcates

new String[]{"a","b","a","d","1","1","e","a"};

How can i do so...

userimage

the best way could be as below please try this:


package com.test.duplicate;

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

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

        if(checkDuplicate(sValue)){
            System.out.println("Duplicated Value Found.");
        }
    }
   
    private static boolean checkDuplicate(String[] arrayValue)
    {
        Set set = new HashSet();
        for(String temp : arrayValue)
        {
            if (set.contains(temp)){
                return true;
            } else{
                set.add(temp);
            }
        }
        return false;
    }
}
Answer is