Skip to content

Instantly share code, notes, and snippets.

View orhanobut's full-sized avatar

Orhan Obut orhanobut

View GitHub Profile
@orhanobut
orhanobut / gist:6637712
Last active December 23, 2015 12:49
Remove duplicates from an integer array and return the array with original order with only primitives. Complexity = O(n^2)
public static int[] removeDuplicates(int[] list) {
int count = 0;
for (int i = 0; i < list.length; i++) {
boolean isExist = false;
int value = list[i];
list[i] = 0;
for (int j = 0; j < count; j++) {
if (list[j] == value) {
isExist = true;
break;
@orhanobut
orhanobut / gist:6637735
Last active December 23, 2015 12:49
Remove duplicated values from an integer array with using sorting. Complexity = O (n.log(n))
public static int[] sortAndRemove(int[] list) {
list = mergeSort(list);
int[] temp = new int[list.length];
temp[0] = list[0];
int j = 1;
for (int i = 1; i < list.length; i++) {
if (list[i] != list[i - 1]) {
temp[j] = list[i];
j++;
}
@orhanobut
orhanobut / gist:6637742
Last active December 23, 2015 12:49
Fibonacci with recursive. Complexity = O (n!)
public static int fib(int n) {
if (n > 1) {
return fib(n - 1) + fib(n - 2);
}
return n;
}
@orhanobut
orhanobut / gist:6637748
Last active December 23, 2015 12:49
Factorial with recursive. Complexity = O (n!)
public static int fact(int n) {
if (n > 1) {
return n * fact(n - 1);
}
return n;
}
@orhanobut
orhanobut / gist:6637761
Last active December 23, 2015 12:49
Reverse a string in-space without an extra buffer. Complexity = O (n)
public static String reverse(String s) {
int lenght = s.length();
int last = s.length() - 1;
char[] temp = s.toCharArray();
for (int i = 0; i < lenght / 2; i++) {
char c = temp[i];
temp[i] = temp[last - i];
temp[last - i] = c;
}
return new String(temp);
@orhanobut
orhanobut / gist:6637780
Last active December 23, 2015 12:49
Remove duplicate char from a string value. Complexity = O (n^2)
public static String removeDuplicateChars(String s) {
if (s == null)
return s;
int len = s.length();
if (len < 2) {
return s;
}
char[] chars = s.toCharArray();
int index = 0;
for (int i = 0; i < len; i++) {
@orhanobut
orhanobut / gist:6637791
Last active December 23, 2015 12:49
Remove duplicate chars from a string for english word. Complexity = O (n)
public static String removeDuplicateChars2(String s)
{
if (s == null) {
return s;
}
int len = s.length();
if (len < 2){
return s;
}
@orhanobut
orhanobut / gist:6637803
Last active December 23, 2015 12:49
Algorithm to check if the given 2 strings are anagram. Complexity = O (n)
public static boolean checkIfAnagram(String s1, String s2) {
int len1 = s1.length();
for (int i = 0; i < len1; i++) {
if (s1.charAt(i) != s2.charAt(len1 - i - 1)) {
return false;
}
}
return true;
}
@orhanobut
orhanobut / gist:6637818
Last active December 23, 2015 12:49
Get the minimum value of a given integer array. Complexity = O (n)
public static int getMin(int[] a) {
if (a == null) {
return 0;
}
if (a.length == 1) {
return a[0];
}
int min = a[0];
for (int i = 0; i < a.length; i++) {
if (a[i] < min) {
@orhanobut
orhanobut / gist:6637825
Last active December 23, 2015 12:49
Get the minimum value of a given sorted integer array. Complexity log(n)
public static int getMinValue(int[] a) {
if (a == null)
return -1;
int lo = 0;
int hi = a.length - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (a[mid] > a[hi]) {
lo = mid + 1;
} else if (a[mid] < a[hi]) {