duplicates

Find any one of multiple possible repeated integers in a list

泪湿孤枕 提交于 2019-11-30 11:47:49
Given an array of n+1 integers, each in the range 1 to n , find an integer that is repeated. I was asked this at a job interview. Here's my answer: The Pigeonhole Principle says there has to be a repeat. I tried to use a binary search approach, so I did this in Matlab, because that's what I know: top = 0; bot = 0; for i=1:n+1 if P[i] > n/2 top = top+1; else bot = bot+1; end So then I argue that one of these, top or bot , has to be bigger than n/2 by the PhP again. Take that range and repeat. I thought this was a pretty good solution, but the interviewer sort of hinted that one can do better.

PHP/MySQL: Getting Multiple Columns With the Same Name in Join Query Without Aliases?

旧时模样 提交于 2019-11-30 11:38:52
I have two tables. One for users and one for posts. The users table has the following fields: id, username, password, created_at, modified_at The posts table has the following fields: id, user_id, title, body, created_at, modified_at When I use a query like: SELECT * FROM `users` LEFT OUTER JOIN `posts` ON users.id=posts.user_id And fetch the results using PDO: $sth = $this->$default_connection->prepare($query); $sth->execute(); $sth->fetchAll(PDO::FETCH_ASSOC); The returned array overwrites all the columns with the same names like id, created_at, modified_at like this: Array ( [0] => Array (

Remove duplicate values from a string in java

試著忘記壹切 提交于 2019-11-30 11:35:34
Can anyone please let me know how to remove duplicate values from String s="Bangalore-Chennai-NewYork-Bangalore-Chennai"; and output should be like String s="Bangalore-Chennai-NewYork-"; using Java.. Any help would be appreciated. This does it in one line: public String deDup(String s) { return new LinkedHashSet<String>(Arrays.asList(s.split("-"))).toString().replaceAll("(^\\[|\\]$)", "").replace(", ", "-"); } public static void main(String[] args) { System.out.println(deDup("Bangalore-Chennai-NewYork-Bangalore-Chennai")); } Output: Bangalore-Chennai-NewYork Notice that the order is preserved

MySQL select rows that do not have matching column in other table

天大地大妈咪最大 提交于 2019-11-30 11:20:05
问题 I can't seem to figure this out so far. I am trying to join two tables and only select the rows in table A that do not have a matching column in table B. For example, lets assume we have a users table and a sent table. users table has the following columns: id, username sent table has the following columns: id, username I want to select all rows from users where username does not exist in sent table. So, if tom is in users and in sent he will not be selected. If he is in users but not in sent

how to avoid duplicates in a has_many :through relationship?

风流意气都作罢 提交于 2019-11-30 10:57:16
问题 How can I achieve the following? I have two models (blogs and readers) and a JOIN table that will allow me to have an N:M relationship between them: class Blog < ActiveRecord::Base has_many :blogs_readers, :dependent => :destroy has_many :readers, :through => :blogs_readers end class Reader < ActiveRecord::Base has_many :blogs_readers, :dependent => :destroy has_many :blogs, :through => :blogs_readers end class BlogsReaders < ActiveRecord::Base belongs_to :blog belongs_to :reader end What I

git tree contains duplicate file entries

ぐ巨炮叔叔 提交于 2019-11-30 09:52:06
I struggled with some line-ending problems about 20 commits back and some weird things happened. Now git fsck shows: Checking object directories 100% (256/256), done. error in tree ee2060e71cb36d33be5ddc1fe9ca8d7dd0ab35cd: contains duplicate file entries Checking objects: 100% (8633/8633), done. and git show ee2060 shows: File1.cs File2.cs File2.cs File2.cs File3.cs This is preventing me from pushing to my remote. git push shows: error: unpack failed: index-pack abnormal exit To https://github.com/username/Project.git ! [remote rejected] master -> master (n/a (unpacker error)) error: failed to

xcode duplicate symbols for architecture error after updating cocoa pods

风格不统一 提交于 2019-11-30 09:49:09
Here is my podFile : source 'https://github.com/CocoaPods/Specs.git' platform :ios, '7.0' pod 'AFNetworking' pod 'ODSAccordionView', '0.4.4' pod 'IQKeyboardManager' pod 'NYXImagesKit', :git => 'https://github.com/Nyx0uf/NYXImagesKit.git' pod 'PEPhotoCropEditor' pod 'CocoaAsyncSocket' pod 'PKRevealController' pod 'Haneke', '~> 1.0' pod 'MBProgressHUD', '~> 0.9.1' pod 'RadioButton' Everythig has been working fine for a long time, but now, when I update my pods ( pod update ) these 3 pods get uptated: AFNetworking CocoaAsyncSocket IQKeyboardManager After that, nothing works anymore. I get more

Remove duplicate values from HashMap in Java

主宰稳场 提交于 2019-11-30 09:48:01
I have a map with duplicate values: ("A", "1"); ("B", "2"); ("C", "2"); ("D", "3"); ("E", "3"); I would like to the map to have ("A", "1"); ("B", "2"); ("D", "3"); Do you know how to get rid of the duplicate values? At present, I get 'java.util.ConcurrentModificationException' error. Thank you. public static void main(String[] args) { HashMap<String, String> map = new HashMap<String, String>(); map.put("A", "1"); map.put("B", "2"); map.put("C", "2"); map.put("D", "3"); map.put("E", "3"); Set<String> keys = map.keySet(); // The set of keys in the map. Iterator<String> keyIter = keys.iterator();

Merge data.frames with duplicates

◇◆丶佛笑我妖孽 提交于 2019-11-30 09:22:00
问题 I have many data.frames, for example: df1 = data.frame(names=c('a','b','c','c','d'),data1=c(1,2,3,4,5)) df2 = data.frame(names=c('a','e','e','c','c','d'),data2=c(1,2,3,4,5,6)) df3 = data.frame(names=c('c','e'),data3=c(1,2)) and I need to merge these data.frames, without delete the name duplicates > result names data1 data2 data3 1 'a' 1 1 NA 2 'b' 2 NA NA 3 'c' 3 4 1 4 'c' 4 5 NA 5 'd' 5 6 NA 6 'e' NA 2 2 7 'e' NA 3 NA I cant find function like merge with option to handle with name duplicates

Sorting an array while moving duplicates to the end?

六月ゝ 毕业季﹏ 提交于 2019-11-30 08:33:40
问题 This was a question in one my friend's programming class. Q. How do you sort an array of int s and then arrange them such that all duplicate elements appear at the end of the array? For example, given the input {5, 2, 7, 6, 1, 1, 5, 6, 2} The output would be {1, 2, 5, 6, 7, 1, 2, 5, 6} Note that the numbers are sorted and duplicate numbers are after 7, which is the maximum in the array. This has to be achieved with out using any Java library packages/utils . I suggested to sort the array