Free Puzzle Questions with Answer using Java, C#, JavaScript and PHP

Java Quiz - Find Longest Substring Count

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package findlongestsubstringcount;

/**
 *
 * @author Bereket-Excellerent
 */
public class FindLongestSubstringCount {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        
        String input = "abcabcbb";
        
        int result = largestSubstringCount(input);
        
        System.out.println(result);
    }
    
    public static int largestSubstringCount(String givenArray){
        String[] characters = new String[givenArray.length()];
        
        int index = 0;
        int maxCounter = 0;
        
        for(int i=0; i<givenArray.length(); i++){
            int duplicateIndex = -1;
            
            if(i > 0){
                duplicateIndex = checkDuplicate(characters, givenArray.substring(i, i+1), index);
            }
            
            if(duplicateIndex == -1){
                characters[index] = givenArray.substring(i, i+1);
                index++;
                
                if(index > maxCounter){
                    maxCounter = index;
                }
            }
            else{
                if(index > maxCounter){
                    maxCounter = index;
                }

                i = i - index + 1;
                index = 0;
                
                characters[index] = givenArray.substring(i, i+1);
                index++;
            }
        }
        
        return maxCounter;
    }
    
    public static int checkDuplicate(String[] array, String character, int length){
        for(int i=0; i < length; i++){
            if(array[i].equals(character) == true){
                return i;
            }
        }
        
        return -1;
    }
}