How to populate two array deleting duplicate numbers - c

I need to populate a first array with 20 numbers then I have to populate a second array using the numbers of the first array but without duplicate.
My question is: which is the best way to do this?
The only way I've found is:
populate the first array
populate the second array using the same numbers
delete duplicates from the second array
but I suppose this is not the best solution. Any suggestion?

homework?
One approach would be to sort the array (search for qsort). Then duplicate elimination is as simple as copying the numbers from array 1 to array 2 skipping all equal numbers.

Related

Getting 10 nsmallest arrays from a set of arrays

First of all, I apologize for the confusing title, the task which I'm trying to accomplish is itself still confusing to me, hence why I'm finding it hard to do it. I'll try to be as clear as I can from now on.
I have 100 500x500 arrays, the values inside range from 0 to 1. What I would like to do is write a code that gives me 10 arrays, these arrays will be a sort of composite of the minimum values between them.
The first array is made of the absolute minimum values, the second array with the 2nd order minimum values....and so on. So the 10 arrays will be a composite of sorted ascending values.
I managed to get the absolute minimum with np.minimum() but I have no clue on how to proceed to the next ones.
To reiterate, I don't want to sort the 100 arrays, but loop through them and create new arrays with the lowest values found in each position.
Sorting is the most efficient way.
np.sort([array0,array1,...], 0)
Will yield an array where the first element is an 100x100 array of the smallest element-wise entries of all your arrays, the second the second smallest, etc.

Find first non-consecutive array element using Google Sheets Filter

I have to make this work in a (always sorted) array that results from splitting a delimited string of sorted whole numbers. Concretely, if my cell contains "1,2,3,5" I need a formula to evaluate to 4. A beneficial side-effect would be to find an implementation that would give the last-number+1, if the original array had only consecutive numbers, i.e., applying the formula to "1,2,3,4,5" would evaluate to 6.
My approach has been to generate a new array that is a perfect sequence and compare it with my original array, to find the first element where the two arrays are not equal.
Creating a perfect sequence of the array like this:
=TRANSPOSE(SEQUENCE(COUNT(arr),1,MIN(arr),1))
So all that would be left to do is compare arr with the sequence above to find the first element that differed, something like:
=COUNTA(IFERROR(FILTER(arr;MATCH(arr; transpose(sequence(count(arr),1,min(arr),1))
;0))))
Sadly, what I have above is not correctly "short-circuiting" at the first non equal value of the arrays. Is COUNTIF the way to go?
If my previous step gets me the index of the element instead of the value, then what remains is to get the value at that index:
INDEX( arr, 1, counta(iferror(filter(arr;match(arr1; transpose(sequence(count(arr),1,min(arr),1))
;0)))) )
Is there a more straight-forward way get the first non-consecutive element? A way that does not involve actual ranges in the spreadsheet?
After some thought, I realized that set-subtracting (i.e. set difference) the original array from the generated sequence always gives me the first missing number.
So my final formula to handle this and the case where all numbers are in sequence is this:
IFERROR(INDEX(FILTER(TRANSPOSE(SEQUENCE(COUNT(SPLIT(G7," ")),1,LEFT(G7,4),1)), ISERROR(MATCH(TRANSPOSE(SEQUENCE(COUNT(split(G7," ")),1,LEFT(G7,4),1)),SPLIT(G7, " "),False))),1,1),RIGHT(G7,4)+1) )
I'm sure there is a better, more concise answer, but this does the job.

Number of ways to create a spiral array

We have an array with values [2,1,5,3,7] and we have to find number of ways to form a spiral array by deleting one element at a time. Lets say when we remove 2 we can have [1,5,3,7] and when we remove 1 then we have [2,5,3,7]. There is no more possibility. so we should return 2. Do you have any idea how can i solve this problem in Java?

Efficient method to sort an array (The method of sorting must be picking an element from the array and placing it elsewhere in the array)

What is the most efficient method I can use to sort an array of integers by selecting one integer at a time and inserting it anywhere in the array? Or, inserting it at the end or the beginning of the array?
I'm looking for an algorithm that can do this in the minimum number of steps, unlike selection sort.
You need to figure out the longest sequence of sorted elements. Then the number of steps is the number of elements that are not in the sequence.
Since the elements that don't belong to the selected sequence are out of order you need to move each of them to the right position. Since each move is a step according to your description, and you already picked the longest sorted sequence, this will give you the least number of steps.

Find the subset of an array such that the elements in the subset have a common difference

I came across a question while preparing for my interview.
Given an array of integers as input.
We have find a possible subset such that the elements in the array have a common difference.
For example,
Consider the input array to be {1,3,7,10,11}
Then the output should be {3,7,11}.
It is always that the elements in the array are in increasing order.
I thought of finding all the subsets and look for a solution.
But that would cause my program to run slower if the input array size is too large.
can anyone help me to crack this???
From what I understand, you want to extract possible subsets from an array such that each two consecutive numbers have the same difference value increasingly.
Here is my algorithm:
Remove duplicates.
Force arrange ascendingly.
Keep a hashtable with the difference values as keys and lists of lists as values.
Loop through the array, updating/adding a key in the hashtable that equals the difference between the two consecutive numbers at hand, and adding a list to the value (the list of lists) containing the two numbers.
After the loop, create an array. Loop through the hashtable, adding an element each time to the array which is an array itself: The merging of all nested lists in the value at hand. This is the array containing all possible subsets.
Here's a possible implementation in python:
from itertools import chain
def find_subsets (array):
table = dict()
last = array[-1]
for num in sorted (set (array), False)[1:]:
diff = last - num
table[diff].append([num, last])
last = num
return [list(chain(v)) for k, v in table]
Please try this code and correct it if wrong. I wrote this in a hurry.

Resources