duplicates

How do you get the duplicate key that ToDictionary() has failed on?

淺唱寂寞╮ 提交于 2019-11-29 02:52:24
问题 I'm creating a Dictionary object, using IEnumerable 's ToDictionary() extension method: var dictionary = new Dictionary<string, MyType> (myCollection.ToDictionary<MyType, string>(k => k.Key)); When it executes, it throws the following ArgumentException : An item with the same key has already been added. How do I get it to tell me what the duplicate key is? 回答1: Get the duplicate keys: var duplicateKeys = myCollection .GroupBy(k => k.Key) .Where(g => g.Count() > 1) .Select(g => g.Key); 回答2: If

Java serialization and duplicate objects

守給你的承諾、 提交于 2019-11-29 01:37:37
I have the following setup: public class A { private Set<C> cSet; } public class B { private Set<C> cSet; } public class C {} A's and B's cSet might have references to same C instances. I want to serialize A and B such that upon deserialization I don't have duplicate C objects. Will Java know how to do the right thing if I serialize/deserialize it into the same ObjectOutputStream, or might I end up with more C instances than I started out with? No, you won't get more instances of C than necessary. That's because internally, the ObjectOutputStream registers every ever serialized object in a

Python - Find same values in a list and group together a new list

风流意气都作罢 提交于 2019-11-29 01:25:32
I'm stuck figuring this out and wonder if anyone could point me in the right direction... From this list: N = [1,2,2,3,3,3,4,4,4,4,5,5,5,5,5] I'm trying to create: L = [[1],[2,2],[3,3,3],[4,4,4,4],[5,5,5,5,5]] Any value which is found to be the same is grouped into it's own sublist. Here is my attempt so far, I'm thinking I should use a while loop? global n n = [1,2,2,3,3,3,4,4,4,4,5,5,5,5,5] #Sorted list l = [] #Empty list to append values to def compare(val): """ This function receives index values from the n list (n[0] etc) """ global valin valin = val global count count = 0 for i in xrange

Remove duplicates from Array without using Hash Table

末鹿安然 提交于 2019-11-29 00:41:21
i have an array which might contain duplicate elements(more than two duplicates of an element). I wonder if it's possible to find and remove the duplicates in the array: without using Hash Table (strict requirement) without using a temporary secondary array. No restrictions on complexity. P.S : This is not Home work question Was asked to my friend in yahoo technical interview Sort the source array. Find consecutive elements that are equal. (I.e. what std::unique does in C++ land). Total complexity is N lg N, or merely N if the input is already sorted. To remove duplicates, you can copy

Java LinkedList - differences between retrieve operations

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 00:28:27
问题 Are there any differences between different methods in each of the following groups of element retrieve operations in LinkedList ? Returning null + removing operations: poll() , pollFirst() . Returning null + not removing operations: peek() , peekFirst() . Throwing exception + removing operations: pop() , remove() , removeFirst() . Throwing exception + not removing operations: element() , getFirst() . Similar duplications exists in insertion methods. If there is no such difference, I would

duplicate symbols for architectures in Xcode

懵懂的女人 提交于 2019-11-28 22:24:24
Here is the error message I receive when compiling ... Ld /Users/ilia3546/Library/Developer/Xcode/DerivedData/MasterDetail-fhgogwnbpzovbtaskgecptdnvgjs/Build/Products/Debug-iphonesimulator/MasterDetail.app/MasterDetail normal i386 cd /Users/ilia3546/Проекты/iDecide setenv IPHONEOS_DEPLOYMENT_TARGET 5.0 setenv PATH "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin" /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -arch i386

Duplicate json property when converting java object to json string using jackson

烂漫一生 提交于 2019-11-28 21:41:33
I have Pojo object, with getAsJson function to return Json string for this object. I use JsonProperty to define json properties in this object. Use writeValueAsString of ObjectMapper to write json string for this object. import org.codehaus.jackson.JsonGenerationException; import org.codehaus.jackson.annotate.JsonIgnore; import org.codehaus.jackson.annotate.JsonIgnoreProperties; import org.codehaus.jackson.annotate.JsonProperty; import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper; @JsonIgnoreProperties(ignoreUnknown=true) public class

How to merge two hashes with no new keys

坚强是说给别人听的谎言 提交于 2019-11-28 21:29:07
How could I merge two hashes that results in no new keys, meaning the merge would merge keys that exist in both hashes? For example, I want the following: h = {:foo => "bar"} j = {:foo => "baz", :extra => "value"} puts h.merge(j) # {:foo => "baz"} I'm looking for a really clean way of doing this as my current implementation is pretty messy. You could remove keys that weren't in the first hash from the second hash, then merge: h.merge j.select { |k| h.keys.include? k } Unlike my edited-out alternative, this is safe if you decide to change it to a merge! or update . Yjerem's answer works in Ruby

How to keep only one row of a table, removing duplicate rows?

99封情书 提交于 2019-11-28 21:25:47
I have a table that has a lot of duplicates in the Name column. I'd like to only keep one row for each. The following lists the duplicates, but I don't know how to delete the duplicates and just keep one: SELECT name FROM members GROUP BY name HAVING COUNT(*) > 1; Thank you. Roee Adler See the following question: Deleting duplicate rows from a table . The adapted accepted answer from there (which is my answer, so no "theft" here...): You can do it in a simple way assuming you have a unique ID field: you can delete all records that are the same except for the ID, but don't have "the minimum ID"

How to check for duplicates in mysql table over multiple columns

风格不统一 提交于 2019-11-28 21:11:23
I have a table of baseball players(all 1000 or so), with fields: mysql> describe person; +-----------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | firstname | varchar(30) | NO | | NULL | | | lastname | varchar(30) | NO | | NULL | | +-----------+-------------+------+-----+---------+----------------+ But I think there are some players that have gotten added in twice. How can I go through and check for how many