Find a Unique Element in a Compound Array

Find a unique element in a compound array

I see what you're attempting. The originating airport would be the only one included in a 0 index of a sub array but not a 1 index of any sub array.

However, in you're solution, you are comparing each combination of 2 sub arrays (including a sub array and itself, by the way...), and then returning the index of list if a[0] != b[1] is ever true for sub array a. This is returning too many results, and you'll always end up returning the last index. For example, 'SEA', the 0 index of the 3rd sub array, does not equal 'BWI', the 1 index of the 0 sub array, so your start_point now equals 3.

I won't do all of your work for you :), but let me suggest this: When you're going through your iterations, keep track of which sub arrays' index 0 ever equals a different sub array's index 1. Your answer will be the only one not included in this list.

Edit: Continue to work through my above suggestion for good practice, but here's a real quick and short solution:

def find_start_point(list)
list.each_with_index do |sub, idx|
return idx if list.flatten.count(sub[0]) == 1
end
end

This works by returning the index of the sub array with an index 0 where there are no other occurrences of that airport (by flattening the entire array and using #count)

Getting unique items from a two dimensional array python

You can simply do something like this

print list(set(zip(*myList)[1]))

EDIT: As per your requirements in the comments

myList=[]

myList.append(("a",0))
myList.append(("b",0))
myList.append(("b",0))
myList.append(("a",1))
myList.append(("a",1))

Dict = {}

for Data in myList :
Dict.setdefault(Data[1], []).append(Data[0])

print [(values[0], key) for key, values in Dict.items()]

Output

[('a', 0), ('a', 1)]

EDIT1: Solution which preserves order

myList=[]

myList.append(("a",0))
myList.append(("b",0))
myList.append(("b",0))
myList.append(("a",2))
myList.append(("a",1))
myList.append(("a",1))

Dict = {}
Unique = []
for Data in myList :
Dict.setdefault(Data[1], []).append(Data[0])
if Data[1] not in Unique: Unique.append(Data[1])

print [(Dict[Data][0], Data) for Data in Unique]

Output

[('a', 0), ('a', 2), ('a', 1)]

How to sum each unique element of an array within an array

a.group_by(&:first).map { |k,v| [k,v.map(&:last).inject(:+),v.size] }
# => [[1, 84, 1], [11, 113, 2], [12, 18, 1], [15, 59, 2]]

How can I create a distinct list of javascript objects, when I need a compound key to make it distinct?

You can create a JSON string to use as a key, like this:

const distinct = [];
const map = new Map();
for (const item of contacts) {
const {name, email, phone} = item;
const key = JSON.stringify({name, email, phone});
if(!map.has(key){
map.set(key, true);
distinct.push(item);
}
}

Since JSON.stringify (spec | MDN) is required to follow ES2015's property order (which many operations aren't), you know that if you create the objects in the same way every time and all of the enumerable properties are "own" properties, the resulting JSON string will be the same for objects with the same name, email, and phone property values (and different if they're different). (That "create the objects in the same way every time" is important, the objects {a: 1, b: 2} and {b: 2, a: 1} result in different JSON strings.)


Side note: If you're just using map.has and map.set(key, true), a Set would probably make more sense:

const distinct = [];
const set = new Set();
for (const item of contacts) {
const {name, email, phone} = item;
const key = JSON.stringify({name, email, phone});
if(!set.has(key){
set.add(key);
distinct.push(item);
}
}

how to select unique elements of a list in q#?

There is no library method for this in Q#, so you'd have to implement it yourself. If the range of the possible numbers is small (up to N), you can allocate an extra array of N elements and mark all the numbers that occur in the input array. Otherwise you can sort the input array and return all numbers which differ from the one right before them and right after them.

That being said, I wonder why do you need to do this in Q#? Q# is a domain-specific language, so a lot of things which are one or two library calls in general-purpose languages can be rather inconvenient to do in Q#. It is typically much easier to do them in C# or F# driver and pass the result to Q# code as a parameter.

Select unique objects based on an attribute

uniq could take a block

my_array.uniq { |obj| [obj.origin.name, obj.destination.name]}
.map { |obj| [obj.origin, obj.destination] }

# or

my_array.map { |obj| [obj.origin, obj.destination] }
.uniq { |pair| pair.map(&:name) }

MongoDB - unique compound index on array of nested documents

You can create a Partial Index that will index only documents that match its query. In this case, match only documents that have a "cell.x" - effectively saying documents which have some elements.

  db.sheets.createIndex({ "cells.x": 1, "cells.y": 1 }, 
{ unique: true, partialFilterExpression: { "cells.x": { $exists: true } } })

Unique compound index on array fields

You're correct that the restrictions of the unique index is as you described. However, that is only true for a singular value field. Once you use unique index on an array, that makes it a unique+multikey index.

Basically, when you create an index on an array field, MongoDB creates an index entry for that document, for every array field in it. This is called a multikey index.

For example, the document:

{a:1, b:[1, 2, 3]}

with the index:

db.test.createIndex({a:1, b:1})

will have three entries in the index, for each array element of b:

{a:1, b:1}
{a:1, b:2}
{a:1, b:3}

Therefore if you create a unique index on field a and b:

db.test.createIndex({a:1, b:1}, {unique:true})

all of these inserts will fail with unique index violation:

> db.test.insert({a:1, b:1})
E11000 duplicate key error collection: test.test index: a_1_b_1 dup key: { : 1.0, : 1.0 }

> db.test.insert({a:1, b:2})
E11000 duplicate key error collection: test.test index: a_1_b_1 dup key: { : 1.0, : 2.0 }

> db.test.insert({a:1, b:3})
E11000 duplicate key error collection: test.test index: a_1_b_1 dup key: { : 1.0, : 3.0 }

> db.test.insert({a:1, b:[1]})
E11000 duplicate key error collection: test.test index: a_1_b_1 dup key: { : 1.0, : 1.0 }

> db.test.insert({a:1, b:[1,2]})
E11000 duplicate key error collection: test.test index: a_1_b_1 dup key: { : 1.0, : 1.0 }

> db.test.insert({a:1, b:[1,2,3]})
E11000 duplicate key error collection: test.test index: a_1_b_1 dup key: { : 1.0, : 1.0 }

This is the reason why you cannot index more than one array field in an index. If you have multiple arrays, the number of index entries would be the multiplies of the length of the two arrays. Your index could easily be larger than your data, and it could take considerable time to scan such a large index.



Related Topics



Leave a reply



Submit