Create an Array with Same Element Repeated Multiple Times

Create an array with same element repeated multiple times

You can do it like this:

function fillArray(value, len) {
if (len == 0) return [];
var a = [value];
while (a.length * 2 <= len) a = a.concat(a);
if (a.length < len) a = a.concat(a.slice(0, len - a.length));
return a;
}

It doubles the array in each iteration, so it can create a really large array with few iterations.


Note: You can also improve your function a lot by using push instead of concat, as concat will create a new array each iteration. Like this (shown just as an example of how you can work with arrays):

function fillArray(value, len) {
var arr = [];
for (var i = 0; i < len; i++) {
arr.push(value);
}
return arr;
}

How to 'repeat' an array n times

You could do this:

var repeated = [].concat(... new Array(100).fill([1, 2, 3]));

That creates an array of a given length (100 here) and fills it with the array to be repeated ([1, 2, 3]). That array is then spread as the argument list to [].concat().

Oh wait just

var repeated = new Array(100).fill([1, 2, 3]).flat();

would be a little shorter.

Create an array with same element repeated multiple times in c++

If all you're trying to do is expand a std::vector as efficiently as possible then you could do something like :

template<typename T>
void expandVector(T& vec, const size_type noOfExpansions)
{
const size_type sizeOfVec = vec.size();
vec.reserve(noOfExpansions * sizeOfVec);
for(int i = 0; i < noOfExpansions; ++i)
{
vec.insert(vec.end(), vec.begin(), vec.begin() + sizeOfVec);
}
}

Unfortunately there isn't anything quite as expressive as [2]*5 for std::vectors in this case, the best you can do is probably to trim a bit of the fat from the above example:

const size_type sizeOfVec = vec.size();
for(int i = 0; i < noOfExpansions; ++i)
{
vec.insert(vec.end(), vec.begin(), vec.begin() + sizeOfVec);
}

You can also cut saving the size if like in your example, you want to copy and then expand.

Repeating element in array x amount of times in same order

Your loop only copies the first element of array. You were supposed to copy all of the elements numRepeats times. Something like,

for (String s : array) {
for (int i = 0; i < numRepeats; i++) {
arr[m++] = s;
}
}

Read that as for each String s in array assign s to position m in arr (and increment m afterward).

How to add same elements to javascript array n times

For primitives, use .fill:

var fruits = new Array(4).fill('Lemon');console.log(fruits);

How to Create an Array with Same Element repeated multiple times in Arduino?

An array is a collection of the same elements, but symbol and count are obviously different things. To group different things together, struct was invented in the earliest days of C

struct {char symbol;  byte count;} input[] = {
{'a', 2}, {'X' ,3} ,{'!', 1}
};

const byte inputcount = sizeof(input)/sizeof(input[0]); // 3 in this test
char expanded[20]; // will get the result

void setup() {
Serial.begin(9600);
char* resultpos = expanded;
for (auto& elem:input) {
for (byte p = 0; p < elem.count; p++) {
*resultpos++ = elem.symbol;
}
}
*resultpos = 0; // to make it a printable char array
Serial.println(expanded); // should give "aaXXX!"
}
void loop() {}

If you prefer, you can use the classic type of for loop as well. But this for each is really nice, IMO.

Repeat every element in array based on object properties

I've upgraded your functions but you can use the map method

function buildName(data){
for (let i = 0; i < data.length; i++){
let numToLoop = data[i].count
let name = data[i].name
for (let z = 0; z < +numToLoop; z++){
nameList.push(name)
}
}
}

Keep information in array when using a form multiple times

I did what @FaneDuru recommended.
I declared in a standard module Public myArray() As String and this is the generic code that will be inside the form below, which also had erros:

Dim k As Integer

Private Sub butt_add_Click()

If Len(Join(myArray)) = 0 Then
k = 0
ReDim myArray(0)
myArray(k) = textBox.Value
Else
k = UBound(myArray) + 1
ReDim Preserve myArray(k)
myArray(k) = textBox.Value
End If

Sheets("Prueba").Cells(k + 1, 1) = myArray(k)

Sample Image

All in all, you can store the information of a text box in an array, which increments the number of elements as many times as the button is pressed.

The last line of the code is just for checking everything works properly.



Related Topics



Leave a reply



Submit