Skip to content

Instantly share code, notes, and snippets.

@ilwsm
Created February 2, 2023 13:45
Show Gist options
  • Save ilwsm/b11d24b916b6dcd485337ca0b0915428 to your computer and use it in GitHub Desktop.
Save ilwsm/b11d24b916b6dcd485337ca0b0915428 to your computer and use it in GitHub Desktop.
unique_in_order
package com.company;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
String order1 = "AAAABBBCCDAABBB";
String order2 = "ABBCcAD";
Integer[] order3 = {1, 2, 2, 3, 3};
System.out.println(unique_in_order(order1));
System.out.println(unique_in_order(order2));
System.out.println(unique_in_order(Arrays.asList(order3)));
}
static List<Character> unique_in_order(String order) {
List<Character> list = new ArrayList<>();
for (char c : order.toCharArray()) {
list.add(c);
}
return unique_in_order(list);
}
static <T> List<T> unique_in_order(List<T> list) {
ArrayList<T> newList = new ArrayList<>();
T e = null;
for (T t : list) {
if (t != e) {
e = t;
newList.add(e);
}
}
return newList;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment