i have a String "iye" and i want make it distinct and also i have a array ["hi", "bye", "bebe"] and i want to make each element of the array and get the distinct characters only so my array would be like this ["hi", "bye", "be"] an then at last i want to take each element from that distinct array and count how many characters of distinctArray[i] are present in the distinct String "iye" and i will store that count for each element of distinct array in same order respectively to the elements of distinct array for e.g
sample input = "iyee" and ["hi", "bye", "bebe"]
sample ouput = [1, 2, 1]
below is my solution not working for larger inputs
static int[] mathProfessor(String B,String[] a){
List<String> distinct = new ArrayList<String>();
int[] arr = new int[a.length];
// store each value of names array as distinct value
for (int i = 0; i < a.length; i++) {
StringBuilder str = new StringBuilder();
a[i].chars().distinct().forEach(c -> str.append((char) c));
distinct.add(str.toString());
}
// System.out.println("distinct list: " + distinct.toString());
// store the count
int count = 0;
for (int i = 0; i < distinct.size(); i++) {
String s = distinct.get(i);
for (int j = 0; j < B.length(); j++) {
if (s.contains(Character.toString(B.charAt(j))))
count++;
}
arr[i] = count;
count = 0;
}
return arr;
}
static int[] mathProfessor(String b, String[] a) {
b = dist(b);
int count = 0;
String[] arr = new String[a.length];
int[] countArr = new int[a.length];
for (int i = 0; i < a.length; i++) {
arr[i] = dist(a[i]);
}
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < b.length(); j++) {
if (arr[i].contains(Character.toString(b.charAt(j))))
count++;
}
countArr[i] = count;
count = 0;
}
//System.out.println(Arrays.toString(countArr));
return countArr;
}
public static String dist(String s) {
StringBuilder sb = new StringBuilder();
Set<Character> set = new HashSet<Character>();
for (int i = 0; i < s.length(); i++) {
if (set.add(s.charAt(i)) == true)
sb.append(s.charAt(i));
}
return sb.toString();
}
Using Java 8+ Streams:
static int[] mathProfessor(String b, String[] a) {
var distinctB = dist(b);
System.out.println(distinctB);
var result = new int[a.length];
for(int i=0, j=a.length; i < j; i++) {
result[i] = (int) Arrays.stream(dist(a[i]).split("")).filter(distinctB::contains).count();
}
return result;
}
public static String dist(String s) {
Set<String> set = new HashSet<>();
set.addAll(Arrays.asList(s.split("")));
return String.join("", set);
}
I am having difficulties for writing an algorithm that will create an array of arrays, from a single array of integers.
Say, I have an
int[] intArray = new int[] {1,2,3,4,5};
What I need from it is an array of an arrays that will look like this:
int[][] array = new int[][]{
{-1,2,3,4,5},
{1,-2,3,4,5},
{1,2,-3,4,5},
{1,2,3,-4,5},
{1,2,3,4,-5};
Thank you in advance!!
EDIT:
The following code works for the case if I want to have only one negative value. How about if I would like to have 2,3,4... negative values? Is there a way to make it more dynamic? For example, from {1,2,3,4,5}; to get: {-1,-2,3,4,5}, {-1,2,-3,4,5}, {-1,2,3,-4,5}, {-1,2,3,4,-5}, {1,-2,-3,4,5},{1,-2,3,-4,5}, {1,-2,3,4,-5}.... or for 3 negative values: {-1,-2,-3,4,5}, {-1,-2,3,-4,5}, {-1,-2,3,4,-5}, {1,-2,-3,-4,5}, {1,-2,-3,4,-5}, {1,2,-3,-4,-5}...etc I hope you get my point! Thanks again guys!!
You could try this:
int l = intArray.length;
int[][] newArray = new int[l][l];
for (int i = 0; i < l; i++) {
for (int j = 0; j < l; j++) {
newArray[i][j] = j == i ? intArray[j] * -1 : intArray[j];
}
}
newArray will have the values you are expecting.
how about
public class x
{
public static int[][] convert(int in[])
{
int s = in.length;
int out[][] = new int[s][s];
for (int i = 0; i < s; ++i) { // row loop
for (int j = 0; j < s; ++j) {
if (i == j)
out[i][j] = -in[j];
else
out[i][j] = in[i];
}
}
return out;
}
public static void print(int in[][])
{
for (int i = 0; i < in.length; ++i) {
String sep = "";
for (int j = 0; j < in[i].length; ++j) {
System.out.print(sep + in[i][j]);
sep = ", ";
}
System.out.println("");
}
}
public static void main(String argv[])
{
int in[] = { 1, 2, 3, 4, 5 };
int out[][];
out = convert(in);
print(out);
}
}
int[] intArray = new int[] {1,2,3,4,5};
int algoIntArray[][] = new int[intArray.length][intArray.length];
for(int i = 0; i < intArray.length; i++){
for (int j = 0; j < algoIntArray[i].length; j++){
if (i == j){
algoIntArray[i][j] = -intArray[j];
} else {
algoIntArray[i][j] = intArray[j];
}
}
}
This code will work for you!
public class Arrays {
private static int[] vals = {1,3,2,5};
public void addBefore(int input, int before){
int[] temp = new int[vals.length*2];
//int[] temp2 = new int [vals.length * 2];
int position = 0;
boolean check = false;
for(int i = 0; i<vals.length; i++){
check = false;
if(vals[i] == (before)){
check = true;
temp[i] = input;
position = i;
break;
}
temp[i] = vals[i];
}
vals[position] = input;
int previous = input;
int current = 0;
for(int i=position;i<vals.length;i++){
current = vals[i];
vals[i] = previous;
previous = current;
}
}
public static void main(String[] args){
Arrays a = new Arrays();
a.addBefore(1, 2);
for(int i = 0; i<vals.length; i++){
System.out.println(vals[i]);
}
}
}
So I am trying to make a addBefore method in Java. It has two parameters input and before. We have to push the input before the given value without replacing anything. After I run this code. The array that is prints out is {1,3,1,1}. I need it to stop replacing and just pushing it forward.
Are you looking for something like this:
public static int[] addBefore(int[] array, int input, int before)
{
//create a new array with 1 more space
int[] newArray = new int[array.length + 1];
//copy in the original array up to the insertion point
for (int x = 0; x < before; x++)
{
newArray[x] = array[x];
}
//element at index 'before' will be pushed one forward; 'before' is now the new one
newArray[before] = input;
//fill in the rest
for (int x = before + 1; x < newArray.length; x++)
{
//need to offset it by one to account for the added int
newArray[x] = array[x - 1];
}
return newArray;
}
public static void main(String[] args)
{
int[] vals = {1,3,2,5};
for(int i = 0; i<vals.length; i++){
System.out.print(vals[i] + ",");
}
System.out.println();
vals = addBefore(vals, 1, 2);
for(int i = 0; i<vals.length; i++){
System.out.print(vals[i] + ",");
}
}
remove vals[position] = input;
public class Arrays {
private static int[] vals = { 1, 3, 2, 5 };
public void addBefore(int input, int before) {
int[] temp = new int[vals.length * 2];
// int[] temp2 = new int [vals.length * 2];
int position = 0;
boolean check = false;
for (int i = 0; i < vals.length; i++) {
check = false;
if (vals[i] == (before)) {
check = true;
temp[i] = input;
position = i;
break;
}
temp[i] = vals[i];
}
// vals[position] = input;
int previous = input;
int current = 0;
for (int i = position; i < vals.length; i++) {
current = vals[i];
vals[i] = previous;
previous = current;
}
}
public static void main(String[] args) {
Arrays a = new Arrays();
a.addBefore(1, 2);
for (int i = 0; i < vals.length; i++) {
System.out.println(vals[i]);
}
}
}
I came across this problem called compress. The objective is to take an array, remove any repeated values and return a new array.
I know this would be very easy with ArrayLists, but I want to do it without them.
So far, I've just written a loop to determine the number of unique values so I can construct a new array of the appropriate length. How can I then get the unique values into the new array?
public static int[] compress(int[] array){
int length = 0;
boolean contains = false;
for (int i = 0; i < array.length; i++){
contains = false;
for (int j = 0; j < i; j++){
if (a[i] == a[j]){
contains = true;
j = i;
} else {
contains = false;
}
}
if (!contains){
length++;
}
}
int[] uniqueArray = new int[length];
}
Not Tested but I think this should do the trick.
public static int[] copyArray(int [] num){
int x = 0;
int numDuplicate = 0;
int[] copy = new int[num.length]; // we use this to copy the non duplicates
HashMap<Integer, Integer> count = new HashMap<>(); //hashmap to check duplicates
for(int i = 0; i < num.length; i++){
if(count.containsKey(num[i])){
count.put(num[i], count.get(num[i])+1);
numDuplicate++; // keep track of duplicates
}else{
count.put(num[i], 1); // first occurence
copy[x] = num[i]; // copy unique values, empty values will be at end
x++;
}
}
// return only what is needed
int newSize = num.length - numDuplicate;
int[] copyNum = new int[newSize];
for(int i = 0; i < copyNum.length; i++){
copyNum[i] = copy[i];
}
return copyNum;
}
public static void main(String[] args) {
// sample elements
int[] nums = new int[20];
for(int i = 0; i < nums.length; i++){
nums[i] = (int)(Math.random() * 20);
}
System.out.println(Arrays.toString(nums));
System.out.println(Arrays.toString(copyArray(nums)));
}
I have BigInt
BigInteger i = new BigInteger("5876934265987526278534978564378568734564937563487564327564376534875483753475");
I need to convert it to [] int. How can I do this fast?
My method is very slow
private static int[] convertDigitsToIntArray(java.math.BigInteger x) {
String s = x.toString();
int[] result = new int[s.length()];
for (int i = 0; i < s.length(); i++) {
result[i] = Integer.parseInt(String.valueOf(s.charAt(i)));
}
return result;
}
Convert it to a string and then iterate the characters:
int[] getArr(BigInteger num)
{
String str = num.toString();
int[] arr = new int[str.length()];
for (int i=0; i<arr.length; i++)
arr[i] = str.charAt(i)-'0';
return arr;
}
May be this will help...
public static void main(String[] args) {
BigInteger i = new BigInteger("5876934265987526278534978564378568734564937563487564327564376534875483753475");
String iStr = i.toString();
int[] intArray = new int[iStr.length()];
for(int j=0; j<iStr.length(); j++) {
intArray[j] = Integer.parseInt(String.valueOf(iStr.charAt(j)));
}
}
Try using this:
private static int[] convertDigitsToIntArray(java.math.BigInteger x) {
String s = x.toString();
int[] result = new int[s.length()];
for (int i = 0; i < s.length(); i++) {
result[i] = s.charAt(i) - '0';
}
return result;
}
You can convert the BigInteger to a String, get the char array of that string and iterate over that:
java.math.BigInteger source = new java.math.BigInteger("5876934265987526278534978564378568734564937563487564327564376534875483753475");
char[] array = source.toString().toCharArray();
int[] result = new int[array.length];
for(int i=0; i<array.length; i++)
{
result[i] = array[i] - '0';
}
private static int[] convertDigitsToIntArray(java.math.BigInteger x) {
String s = x.toString();
int[] result = new int[s.length()];
for (int i = 0; i < s.length(); i++) {
// Change your code to subtract the char '0'
result[i] = s.charAt(i) - '0';
}
return result;
}