A simple approach would be to take sequences of 3 and put them in a HashTable
. As soon as you come across sequence 3, you increase the corresponding counter of counter events. In the end, just return the most frequent appearance / sequence. This can be found by scanning the HashTable
to record with the maximum occurrence value. An example in Java:
public class Sequence { public List<String> sequenceOfThree(List<List<String>> names){ Map<List<String>, Integer> map = new HashMap<List<String>, Integer>(); for(List<String> nameList:names){ int startIdx = 0; int endIdx = 3; while(endIdx <= nameList.size()){ List<String> subsequence = nameList.subList(startIdx, endIdx); //add to map Integer count = map.get(subsequence); if(count == null){ count = 0; } map.put(subsequence, count + 1); startIdx++; endIdx++; } } Integer max = Integer.MIN_VALUE; List<String> result = Collections.emptyList(); for(Entry<List<String>, Integer> entries:map.entrySet()){ if(entries.getValue() > max){ max = entries.getValue(); result = entries.getKey(); } } return result; } /** * @param args */ public static void main(String[] args) { List<List<String>> names = new ArrayList<List<String>>(); names.add(Arrays.asList(new String[]{"Ana", "John", "Maria"})); names.add(Arrays.asList(new String[]{"Paul"})); names.add(Arrays.asList(new String[] "Sharon", "Ana", "John", "Maria", "Tiffany" ,"Ted"})); System.out.println(new Sequence().sequenceOfThree(names)); } }
source share