Quick actions

cmd+k|ctrl+k

Navigation

Languages

longest common prefix

Snippet info

Language

JavaScript

Visibility

public

Author

rohit-sh1

Created

2019-09-18T05:04:43Z

Updated

2019-09-19T02:28:05Z

// Given a array of X number of strings, find the longest common prefix among all strings present in the array.

const myinput = ['pea', 'pear', 'apple', 'for', 'april', 'apendix', 'peace', 1];

/*
    Brute Force Way
    manually check for answers, confirm what the answer should be. in this case it should be ['pea', 'ap'] because both have same number
    loop over all elements and check for 

*/
function findLongestPrefix(arrayinput) {
    const response = "";
    //arrayinput = arrayinput.sort();
    
    for (i=0; i< arrayinput.length; i++) {
        
    }
    
    return response;
}

findLongestPrefix(myinput);
INFO