How to initialize an array of arrays in awk?

If you have GNU awk, you can use a true multidimensional array. Although this answer uses the split() function, it most certainly doesn’t abuse it. Run like: awk -f script.awk Contents of script.awk: BEGIN { x=SUBSEP a=”Red” x “Green” x “Blue” b=”Yellow” x “Cyan” x “Purple” Colors[1][0] = “” Colors[2][0] = “” split(a, Colors[1], x) … Read more

Convert Array to string in Java/Groovy

Use join, e.g., tripIds.join(“, “) Tangential If you want to create a list from another list you generally want something like map or collect as opposed to manually creating a list and appending to it, e.g. (untested): def sql = Sql.newInstance(“jdbc:mysql://localhost:3306/steer”, “root”, “”, “com.mysql.jdbc.Driver”) def tripIds = sql.map { it.id } Or if you only … Read more

Find the element repeated more than n/2 times

There is a beautiful algorithm for solving this that works in two passes (total time O(N)) using only constant external space (O(1)). I have an implementation of this algorithm, along with comments including a correctness proof, available here The intuition behind the algorithm is actually quite beautiful. Suppose that you were to have a roomful … Read more

tech