Uses binary searching to check if a String is already in a String[]. - Java Data Structure

Java examples for Data Structure:Binary Search

Description

Uses binary searching to check if a String is already in a String[].

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        String s1 = "java2s.com";
        String[] arr = new String[] { "1", "abc", "level", null,
                "java2s.com", "asdf 123" };
        System.out.println(duplicateInsertion(s1, arr));
    }//from w ww .  j  a  v a 2s  .  c o  m

    /**
     * Uses binary searching to check if a String is already in a String[].
     * Upper and lower case words that are the same are considered duplicates.
     * ie Hat and hat are duplicates.
     * @param s1
     * @param arr
     * @return
     */
    private static boolean duplicateInsertion(String s1, String[] arr) {
        for (String s : arr)
            if (s1.toLowerCase().equals(s.toLowerCase()))
                return true;
        return false;
    }
}

Related Tutorials