Questions tagged [array]

An array is an ordered data structure consisting of a collection of elements (values or variables), each identified by one (single dimensional array, or vector) or multiple indexes.

Filter by
Sorted by
Tagged with
5
votes
1answer
293 views

(Codewars) Range Extraction

Kata: https://www.codewars.com/kata/range-extraction/python A format for expressing an ordered list of integers is to use a comma separated list of either individual integers or a range ...
2
votes
0answers
931 views

Deepest pit of an array

Following is based on a problem description from a Codility test, as a task in an interview. DeepestPit - problem description A non-empty zero-indexed array B ...
2
votes
2answers
71 views

Split an array of items into several arrays, given a condition

The question is very simple : I have an array of items, that all have a date. According to the date, I would like to split this array into several arrays. In my case, I would like to split the array ...
4
votes
1answer
80 views

Code to replace list of integers with nearest bigger element

The original question was in the context of an array, but I just used a list without changing the length for easier debugging purposes of toString(). Given an ...
1
vote
0answers
54 views

Simple dynamic array template

For my game engine I needed a simple templated array class that can be safely passed across module boundaries. It's not supposed to be a replacement for std::vector and thus doesn't need all of its ...
0
votes
2answers
33 views
5
votes
2answers
560 views

User input happy birthday program

For self-study homework I made a Java program that takes user input, creates people as objects, and says "Happy Birthday" to each person. I am just wondering what mistakes I made here or how it could ...
2
votes
2answers
43 views

Iterating through two objects with different structures in parallel

I have two objects, one input = Array<Type> and one stored = Array<{..., Type}> What's the best (cleanest) way to ...
5
votes
5answers
765 views

Checking if an integer permutation is cyclic in Java

A an integer permutation array[] is said to be cyclic if and only if we can choose an arbitrary element at position i, move it ...
1
vote
2answers
41 views

Selecting lowest position from 'array'

My goal is to move a 'monster' (mX, mY) in a 2d grid towards the player (pX, pY). The monster can move 8 directions. I have ...
1
vote
1answer
136 views

Calculating array size in C++

This code shows two ways to calculate size of array of any type. I would like to know, which should be preferred? Is there any advantage/disadvantage? ...
7
votes
3answers
182 views

C++14 std::array implementation

I invested an hour or so to reimplement the C++ header <array>. Most stuff is in the namespace my_std, but ...
1
vote
0answers
45 views

Filtering users in socket.io and seeing who is online

While implementing socket.io on my server, I ran into some problems and eventually fixed them. The code below is fired on the connection event of socket.io: ...
1
vote
1answer
67 views

Find closest greater and smaller number from a sorted array

I have a sorted array. It can be empty. I have to find its closest greater and smaller number. If the given number is present in the array, it will be considered as greater number. I implemented it ...
0
votes
1answer
48 views

Reduce a grid array [][] into a string

I have a class with a property of grid[][] and each value is either true or false. I need to convert this to a string with letters o and b in place of the boolean. ...
0
votes
1answer
35 views

Two functions that move array elements [closed]

These two functions act on a stack, an array of integers (whose first element is the top of the stack, LIFO). The first function retrieves the first element and puts it at the end. The other is the ...
0
votes
2answers
93 views

Filter non-duplicate objects from collection

I need to filter an array of form controls to check for duplicate values. I have written the following algorithm to identify and return an array of the controls which have are duplicates. I have not ...
1
vote
1answer
98 views

Products excluding each element of the array

Given an array of integers, return a new array such that each element at index i of the new array is the product of all the numbers in the original array except the one at i. For example, if our ...
3
votes
3answers
107 views

Check if a postcode is in a list

I'm looking for improvement / advice / reviews on how I can improve this code. I feel like there is a better way or more efficient way of doing this and I am over looking it. The Plugin I made is ...
1
vote
2answers
102 views

Recursive Transversal (Python)

For a programming challenge I was doing, I needed to traverse a list of lists, and while writing the standard 2D traversal function, I thought why not generalise it, so I did. Here's my best effort: ...
3
votes
2answers
92 views

Efficient method to look for an instance of an object in a collection

I'm trying to determine if an instance of an object exists within a collection. I'm using array_walk_recursive to walk and check the leaves of an array. I've added a small short circuit, but still ...
3
votes
2answers
99 views

Storing computed values in an array [closed]

In several places I need to compute the next value, given a N: double size = 1.0 / Math.Pow(2, N); Since I won't be computing this value for N > 15 I have ...
4
votes
2answers
115 views

Sorting an array of numbers by descending frequency

Somebody asked me to create a code that orders unique numbers of an array according to their frequency, ie: {1,3,3,4,4,4,4,4,2,2,5,5,5,5} to ...
0
votes
4answers
146 views

Counting number of pairs in an integer array

So, I wanted to have the count for number of pairs of same integer there are in an array for which I used a HashMap Input contains two lines: Integer n: Signifies the size of input Input ...
2
votes
1answer
83 views

HackerRank - Left Rotation in JavaScript

I'm looking to learn more about data structures, time complexity, and efficient algorithms. I have solved the Left Rotation Problem on HackerRank using JavaScript. I am looking to see ways to optimize ...
2
votes
1answer
682 views

Rotate matrix 90 degrees

I recently did problem 1.7 Rotate Matrix in Cracking the Coding Interview. I realized my code is very different than what is in the book and what I am also fining online (I'm having trouble following ...
1
vote
1answer
48 views

Monitoring navigation function: logic judgment

Write a simple monitoring navigation function. I feel logic judgment still needs to be optimized. I don't have a clear idea right now about code logic such as reducing ...
4
votes
1answer
145 views

Sorting an array in C++

This started as a simple exercise in sorting an array. I wanted to make it a bit more complex which is why I made it a 2D array. I then noticed that each pass that checked and swapped values, it was ...
2
votes
2answers
99 views

Shuffle array elements by rearranging the key value associations

PHP's shuffle() function destroys the array keys, so I decided to write a shuffle function that doesn't do that and instead rearranges key-value associations. As I ...
2
votes
2answers
42 views

Compare an array with a file and form groups from elements of an array

I have a text file with letters (tab delimited), and a numpy array (obj) with a few letters (single row). The text file has rows with different numbers of columns. ...
1
vote
1answer
35 views

Create an Array of Objects for Patch Notes in React

I was looking to get some reviews of my code, so I can improve my writing. Functionally it works the exact way I want it to based on my patch notes .md file. I'm open to all suggestions for improving ...
12
votes
2answers
192 views

C++ data type to store and manipulate individual bits

The use cases I’m facing require me to have a C++ data type that can store and manipulate the state of somewhere between 400 and 500 bits. In addition it is necessary to be able to store that state ...
4
votes
2answers
70 views

Replace array element with multiplication of neighbors in Scala

Given an array of integers, update the index with multiplication of previous and next integers, Input: 2 , 3, 4, 5, 6 Output: 2*3, 2*4, 3*5, 4*6, 5*6 Following ...
1
vote
2answers
562 views

Write byte array to end of file

I'm writing a byte array at the end of file. The code works fine, but can you please suggest performance improvements? ...
1
vote
1answer
56 views

A 'slow sort': repeatedly swap the first misordered pair

A 'slow sort' is probably the slowest practical sort. It compares each element with the elements after it, and swaps immediately if one is greater than the other, and goes back to the start of the ...
1
vote
1answer
46 views

Wrapping certain elements in an array with an array with PHP

I have a function that's doing this correctly, but I feel as though this can be done (possibly) with minimal code, with recursion, but I'm just wracking my brain. Basically I have JSON that gets ...
3
votes
3answers
220 views

Select data from csv and make associative array

I've got a .csv file with ZIP codes containing the city and its sub-districts 67401,City 1,district_a 67401,City 2,district_b 67401,City 3,district_c 67401,City 3,district_d 67401,City 3,district_e ...
4
votes
2answers
78 views

MaxHeap implementation in JavaScript

Here is MaxHeap implementation in pure JavaScript: ...
2
votes
0answers
77 views

Project Euler #81 in Haskell: minimal path sum through a matrix

I just completed Project Euler Problem 81 in Haskell: In the 5 by 5 matrix below, ...
3
votes
2answers
59 views

I/O-TxtFile Line Distance Program

I am trying to write a lab/program that loops through a .txt input file. Reads an entire line of data as a string, then splits that string into an array delimited by a space. If there are 4 items in ...
1
vote
2answers
71 views

Printing elements of an array in C

I'm trying to print the elements of an array in a fashion that resembles thism where SIZE is a constant: ...
3
votes
1answer
101 views

List of word to list of index in a vocabulary [closed]

I have a dataset composed of ~700k samples. Each sample is composed of n_features features. Each feature is a word. Each feature has his own vocabulary. The size of the vocabularies range from 18 to ...
1
vote
1answer
180 views

Longest Substring Without Repeating Characters in Scala

Question is taken from Leetcode and solution works for their test cases. I do see a scope of improvement and make it more functional (getting rid of vars and mutables data structures). Please suggest ...
4
votes
1answer
104 views

Finding decreasing sequences in an array of numbers

Ants Requirement Given a natural number \$n\$, representing the number of days the study is made, and then a number \$x\$ of \$n\$ natural numbers, representing the number of working ant ...
1
vote
1answer
84 views

Get the most frequent number and least frequent duplicate in array

This code gets the most frequent number and least duplicate number in an array. How can I optimize it and improve its performance? ...
3
votes
1answer
148 views

Checking whether given array is sorted by divide-and-conquer

I've written a code that I try to use divide and conquer approach to determine if the given array is sorted. I wonder whether I apply the approach accurately. ...
-2
votes
1answer
56 views

Append to array if not empty [closed]

I want to call a series of functions to build up an array, like so: ...
-4
votes
1answer
123 views

Bidirectional bubble sort using goto [closed]

I am aware of Dijkstra's paper on why goto is harmful, but I thought it would still be fun to make a bidirectional bubble sort using ...
1
vote
2answers
333 views

A scalable way to remove duplicates from Arrays without extensions in Swift 4

This method is an attempt at removing duplicates from arrays with scalability in mind in Swift 4 without the use of extensions. I am asking for any suggestions, criticisms, and/or observations. ...
1
vote
1answer
195 views

Recursive trim() function to handle arrays and objects

I have come up with a recursive function which applies trim() recursively to all string members of an array/object. I don't have any specific quandaries about my ...