now i get it, so how miners figure out this problem? I mean how they can make sure that they are not mining a transaction which is mined before?
Miners (or mining pools) keep track of the current list of unspent transaction outputs (UTXO), and the list of current unconfirmed transactions.
Whenever a miner (or pool) hears about a new transaction, it checks to make sure that all the inputs to the transaction are in the UTXO. If there are any inputs that are not in the UTXO, then the transaction is invalid and is rejected. This means that a miner (or pool) will only keep the first transaction that it hears about with any given transaction input. Whenever a valid transaction is received, the miner (or pool) removes the transaction's inputs from their UTXO. Whenever a transaction is dropped from the memory pool, the inputs of that dropped transaction are added back into the UTXO.
The miner selects the transactions for their block from their list of unconfirmed transactions. Most other miners (or pools) will be selecting the same transactions for their own blocks. Then all the miners (and pools) on the entire network attempt to complete the proof-of-work. Once a miner (or pool) is successful in completing their block, they broadcast it to the network. As each miner (or pool) receives the block, they verify that it is valid. If it is not, then they reject it and continue working on their own block. If the block is valid, then they remove all the transactions that are in the block from their own list of unconfirmed transactions (and all the inputs that are in the block from their own UTXO). Then they build a new block with the transactions that remain in their memory pool and continue their proof-of-work with the new block that they've just created.
Occasionally, two miners (or pools) will broadcast a solved block at nearly the same time. Each node on the network will use whichever of the two block it receives first, and the other block will be rejected as invalid. The entire network will be split as to which block they believe is the "real" block. Eventually a miner will solve the next block on top of whichever block they thought was the "real" one. Since their chain will now be 1 block longer than the competing chain, it will "win". All the miners, pools, and nodes on that had been working on top of the other block will abandon it. They will add all the transactions from their old block back to their memory pool, and all the inputs back to their UTXO. They'll then update their UTXO and memory pool based on the two block they receive from the now longer chain. The abandoned block is commonly called an "orphaned" block.