Last active
January 22, 2021 15:01
-
-
Save rjmoggach/b1fd0a73dd49d937f3d77fc02f69e3d0 to your computer and use it in GitHub Desktop.
VEX Array Functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function int[] swap (int arr1[]; int arr2[]) { | |
int temp[] = arr1; | |
arr1 = arr2; | |
arr2 = temp; | |
return {1}; | |
} | |
function int swap (int x; int y) { | |
int tmp = x; | |
x = y; | |
y = tmp; | |
return 1; | |
} | |
function int[] unique_elements (int arr[]) { | |
int _arr[] = sort(arr); | |
for (int i=0; i<len(_arr); i++) { | |
while(_arr[i] == _arr[i+1]) pop(_arr,i); | |
} | |
return _arr; | |
} | |
function int[] duped_elements (int arr[]) { | |
int _arr[] = sort(arr); | |
int result[] = {}; | |
int dupe; | |
for (int i=0; i<len(_arr); i++) { | |
while(_arr[i] == _arr[i+1]) { | |
dupe = pop(_arr,i); | |
} | |
append(result, dupe); | |
} | |
return result; | |
} | |
function int[] common_elements (int arr1[]; int arr2[]) { | |
int result[]; | |
foreach (int i; unique_elements(arr1)) { | |
foreach (int j; unique_elements(arr2)) { | |
if (i==j) push(result,j); | |
} | |
} | |
return result; | |
} | |
function int[] different_elements (int arr1[]; int arr2[]) { | |
int result[]; | |
int _arr1[] = unique_elements(arr1); | |
int _arr2[] = unique_elements(arr2); | |
foreach(int i; _arr1) { | |
if(find(_arr2,i)<0) append(result,i); | |
} | |
foreach(int j; _arr2) { | |
if(find(_arr1,j)<0) append(result,j); | |
} | |
return result; | |
} | |
function int[] inside_elements (int src[]; int search[]) { | |
int result[]; | |
int _src[] = unique_elements(src); | |
int _search[] = unique_elements(search); | |
foreach(int i; _search) { | |
if(find(_src,i)>=0) append(result,i); | |
} | |
return result; | |
} | |
function int[] merge_arrays ( int arr1[]; int arr2[]) { | |
int result[]; | |
append(result,arr1); | |
append(result,arr2); | |
return unique_elements(result); | |
} | |
function int[] remove_from (int arr1[]; int arr2[]) { | |
int result[]=arr2; | |
int _arr1[]=unique_elements(arr1); | |
foreach(int i; _arr1) { | |
while(removevalue(result,i)){ | |
continue; | |
} | |
} | |
return result; | |
} | |
function int count_elements (int arr[]; int elem ) { | |
int results[] = find(arr, elem); | |
return len(results); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment