database.merge in apex
List<Account> accList = new List<Account>{new Account(Name='Myacc1'),new Account(Name='MyAcc2')};
insert accList;
Account masterAcct = [SELECT Id, Name FROM Account WHERE Name = 'Myacc1' LIMIT 1];
Account mergeAcct = [SELECT Id, Name FROM Account WHERE Name = 'MyAcc2' LIMIT 1];
try {
merge masterAcct mergeAcct;
} catch (DmlException e) {
// handle the exception
}