Skip to content

Instantly share code, notes, and snippets.

Orhan Obut orhanobut

Block or report user

Report or block orhanobut

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@orhanobut
orhanobut / gist:6637712
Last active Dec 23, 2015
Remove duplicates from an integer array and return the array with original order with only primitives. Complexity = O(n^2)
View gist:6637712
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 Dec 23, 2015
Remove duplicated values from an integer array with using sorting. Complexity = O (n.log(n))
View gist:6637735
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 Dec 23, 2015
Fibonacci with recursive. Complexity = O (n!)
View gist:6637742
public static int fib(int n) {
if (n > 1) {
return fib(n - 1) + fib(n - 2);
}
return n;
}
@orhanobut
orhanobut / gist:6637748
Last active Dec 23, 2015
Factorial with recursive. Complexity = O (n!)
View gist:6637748
public static int fact(int n) {
if (n > 1) {
return n * fact(n - 1);
}
return n;
}
@orhanobut
orhanobut / gist:6637761
Last active Dec 23, 2015
Reverse a string in-space without an extra buffer. Complexity = O (n)
View gist:6637761
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 Dec 23, 2015
Remove duplicate char from a string value. Complexity = O (n^2)
View gist:6637780
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 Dec 23, 2015
Remove duplicate chars from a string for english word. Complexity = O (n)
View gist:6637791
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 Dec 23, 2015
Algorithm to check if the given 2 strings are anagram. Complexity = O (n)
View gist:6637803
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 Dec 23, 2015
Get the minimum value of a given integer array. Complexity = O (n)
View gist:6637818
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 Dec 23, 2015
Get the minimum value of a given sorted integer array. Complexity log(n)
View gist:6637825
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]) {
You can’t perform that action at this time.